gson解析json

用xUtils联网获得json数据后,放到HiJson工具中,点击格式化JSON字符串。


得到上图的格式。

首先写bean,按层一层一层的写,不会乱:

根据右侧的key-value可以看出,该json包括两个数组和一个整型的数字,定义时字段名要与key值一样

public class NewsCenterCategory {

	public List data;
	public List extend;
	public int retcode;
}

点开data,得到下图:


由于数组里面放的是对象,所以定义新的类,并且data的数组中存放的就是该类的对象:

public class NewsCenterCategory {

	public List<CenterCategory> data;
	public List extend;
	public int retcode;
	
	public static class CenterCategory{
		
	}
}
然后,沿着该类往下看,点击[0],得到下图:


</pre><pre name="code" class="java">public class NewsCenterCategory {

	public List<CenterCategory> data;
	public List extend;
	public int retcode;
	
	public static class CenterCategory{
		public List children;
		public int id;
		public String title;
		public int type;
	}
}

接下来依次点击[1]、[2]、[3]、[4]查看是否有不同的key-value,不同的也定义到CenterCategory类中。

public class NewsCenterCategory {

	public List<CenterCategory> data;
	public List extend;
	public int retcode;
	
	public static class CenterCategory{
		public List children;
		public int id;
		public String title;
		public int type;
		public String url;
		public String url1;
		public String dayurl;
		public String excurl;
		public String weekurl;
	}
}

接下来处理extend数组,点击extend,得到下图:


得知该数组为int型数组:

public class NewsCenterCategory {

	public List<CenterCategory> data;
	public List<Integer> extend;
	public int retcode;
	
	public static class CenterCategory{
		public List children;
		public int id;
		public String title;
		public int type;
		public String url;
		public String url1;
		public String dayurl;
		public String excurl;
		public String weekurl;
	}
}

然后还有一个List,点击children:


该数组里面存的也是对象,定义新的类,后面的步骤如③④,最后得到bean

public class NewsCenterCategory {

	public List<CenterCategory> data;
	public List<Integer> extend;
	public int retcode;
	
	public static class CenterCategory{
		public List<CenterCategoryItem> children;
		public int id;
		public String title;
		public int type;
		public String url;
		public String url1;
		public String dayurl;
		public String excurl;
		public String weekurl;
	}
	
	public static class CenterCategoryItem{
		public int id;
		public String titel;
		public int type;
		public String url;
	}
}

写完bean后,在xUtils联网成功的函数处,调用gson来解析json:

@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
    LogUtils.i(responseInfo.result);
    Gson gson = new Gson();
    NewsCenterCategory newsCenterCategory = gson.fromJson(responseInfo.result, NewsCenterCategory.class);
}

之后就可以对解析后的json数据做其他处理了。


由于需要对json解析的地方很多,所以我们可以把Gson解析json写成一个工具类

public class GsonUtils {

	public static <T>T jsonToBean(String jsonResult,Class<T> clazz){
		Gson gson = new Gson();
		T t = gson.fromJson(jsonResult, clazz);
		return t;
	}
}

然后,调用GsonUtils工具类来解析json:

@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
    LogUtils.i(responseInfo.result);
    //Gson gson = new Gson();
    //NewsCenterCategory newsCenterCategory = gson.fromJson(responseInfo.result, NewsCenterCategory.class);
    NewsCenterCategory newsCenterCategory = GsonUtils.jsonToBean(responseInfo.result, NewsCenterCategory.class);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值