gson解析json

gson解析json


public class Student 
{
	public String name;
	public int age;
	public boolean sex;
}



Object转json串:

	Gson gson = new Gson();
	String json = gson.toJson(student);

json转Object:

	private static void parser2Object() 
	{
		// json转换 为简单对象
		String json = "{'name':'张三','age':18,'sex':true}";
		Gson gson = new Gson();
		Student student = gson.fromJson(json, Student.class);//
		System.out.println(student);
	}


json转List:

	private static void parser2List() 
	{
		String json = "[{'name':'刘1','age':18,'sex':true},{'name':'刘2','age':19,'sex':false}]";
		Gson gson = new Gson();
		TypeToken<List<Student>> token = new com.google.gson.reflect.TypeToken<List<Student>>() {};//
		List<Student> stus = gson.fromJson(json, token.getType());
	}


json转Map:

	private static void parser2Map() 
	{
		String json = "[{'name':'刘1','age':18,'sex':true},{'name':'刘2','age':19,'sex':false}]";
		Gson gson = new Gson();
		TypeToken<Map<String, Student>> token = new com.google.gson.reflect.TypeToken<Map<String, Student>>() {};
		Map<String, Student> stus = gson.fromJson(json, token.getType());
	}

json先节点解析,在转换:

	private static void parser2() 
	{
		// 节点解析
		String json = "{'status':200,'data':{'name':'张三','age':18,'sex':true}}";
		// 1.获得 解析者
		JsonParser parser = new JsonParser();
		// 2.获得 根节点元素
		JsonElement element = parser.parse(json);
		// 3.根据 文档判断根节点属于 什么类型的 Gson节点对象
		JsonObject root = element.getAsJsonObject();
		// 4. 取得 节点 下 的某个节点的 value
		JsonPrimitive flagJson = root.getAsJsonPrimitive("status");
		int asInt = flagJson.getAsInt();// 请求返回判断
		JsonObject dataJson = root.getAsJsonObject("data");
		// (JsonElement, Class<T>) element->obj/arr  
		Student student = new Gson().fromJson(dataJson, Student.class);
		root.getAsJsonArray();
	}


嵌套类解析:

public class NewsBean 
{
	public int code;
	public ArrayList<NewsMenuData> data;

	public class NewsMenuData
	{
		public String id;
		public String title;
		public ArrayList<NewsTabData> children;
		// toString...
	}

	public class NewsTabData 
	{
		public String id;
		public String title;
		// toString...
	}
	// toString...
}

		{
		    'code': 1,
		    'data': [
		        {
		            'id': 1,
		            'title': 'news',
		            'children': [
		                {
		                    'id': 1,
		                    'title': 'dataTitle'
		                },
		                {
		                    'id': 1,
		                    'title': 'dataTitle'
		                }
		            ]
		        },
		        {
		            'id': 2,
		            'title': 'news',
		            'children': [
		                {
		                    'id': 1,
		                    'title': 'dataTitle'
		                },
		                {
		                    'id': 1,
		                    'title': 'dataTitle'
		                }
		            ]
		        }
		    ]
		}

		String json = "{'code':1,'data':[{'id':1,'title':'news','children':[{'id':1,'title':'dataTitle'},{'id':2,'title':'news','children':[{'id':1,'title':'dataTitle'}]},{'id':1,'title':'news','children':[{'id':2,'title':'dataTitle'},{'id':2,'title':'news','children':[{'id':1,'title':'dataTitle'}]}]}";
		Gson gson = new Gson();
		NewsBean student = gson.fromJson(json, NewsBean.class);//





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值