GSON解析JSON例子

1.Maven Dependency

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.fool.json.gson</groupId>
	<artifactId>gson</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>GSON</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.2.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.1</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

 

2.POJO

Person.java

package org.fool.json.gson;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class Person
{
	private String username;

	private String password;

	private int age;

	private String address;

	private List<String> list = new ArrayList<>();

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}

	public String getAddress()
	{
		return address;
	}

	public void setAddress(String address)
	{
		this.address = address;
	}

	public List<String> getList()
	{
		return list;
	}

	public void setList(List<String> list)
	{
		this.list = list;
	}

	@Override
	public String toString()
	{
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}
}

 

3.JUnit Test

GsonTest.java

package org.fool.json.gson;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

public class GsonTest
{
	private Gson gson = null;
	private Person person = null;

	@Before
	public void setUp()
	{
		gson = new GsonBuilder().create();

		person = new Person();

		person.setUsername("zhangsan");
		person.setPassword("123456");
		person.setAddress("beijing");
		person.setAge(30);

		person.getList().add("hello");
		person.getList().add("world");
		person.getList().add("Hero");
		person.getList().add("Heroine");
	}

...
}

 

toJsonTest

控制台输出通过GSON解析过的JSON格式的person数据,并存入gsonResult.json的文件中

...
    @Test
	public void toJsonTest()
	{
		gson.toJson(person, System.out);
		
		String result = gson.toJson(person);

		BufferedWriter bw = null;

		try
		{
			bw = new BufferedWriter(new FileWriter("gsonResult.json"));

			bw.write(result);

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				bw.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
...

 result

{"username":"zhangsan","password":"123456","age":30,"address":"beijing","list":["hello","world","Hero","Heroine"]}

 

fromJsonTest

通过GSON解析gsonResult.json文件,将JSON格式的数据转换成Person对象

...
    @Test
	public void fromJsonTest()
	{
		BufferedReader br = null;

		try
		{
			br = new BufferedReader(new FileReader("gsonResult.json"));

			Person person = gson.fromJson(br, Person.class);

			System.out.println(person);
		}
		catch (JsonSyntaxException e)
		{
			e.printStackTrace();
		}
		catch (JsonIOException e)
		{
			e.printStackTrace();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				br.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
...

 result

Person[username=zhangsan,password=123456,age=30,address=beijing,list=[hello, world, Hero, Heroine]]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值