本文主要介绍json最原始的解析与google提供的gson工具类解析
①json解析
/**
* 普通的json解析
* @param s
* @throws JSONException
*/
private void jsonJieXi(String s) throws JSONException {
//创建json对象
JSONObject jsonObject1 = new JSONObject(s);
String retcode = jsonObject1.getString("retcode");
String header = jsonObject1.getString("header");
Log.i(TAG, "retcode=" + retcode + "----------header=" + header); JSONArray data = jsonObject1.getJSONArray("data"); for (int i = ; i < data.length(); i++) {
JSONObject obj = (JSONObject) data.get(i);
String ids = (String) obj.get("id");
String title = (String) obj.get("title");
String type = (String) obj.get("type");
String des = (String) obj.get("des");
Log.i(TAG, "ids=" + ids + "--title=" + title + "--type=" + type + "--des=" + des + "\n");
}
}
②gson解析
1)首先在AndroidStudio中安装一个GsonFormat插件
2)新建一个javaben类然后按下组合键alt+insert 把完整的json数据拷贝到编辑框中
3)添加gson的依赖包
4)然后生成Gson指定格式的java ben
import java.util.List; /**
* 作者:AdminHeJun.
* 时间:2017/9/3 19:28.
* 邮箱:1270960250@qq.com
* 内容:
* 修改:
*/ public class NewsInfo { private int retcode;
private String header;
private List<DataBean> data; public int getRetcode() {
return retcode;
} public void setRetcode(int retcode) {
this.retcode = retcode;
} public String getHeader() {
return header;
} public void setHeader(String header) {
this.header = header;
} public List<DataBean> getData() {
return data;
} public void setData(List<DataBean> data) {
this.data = data;
} public static class DataBean {
/**
* id : 10000
* title : 新闻
* type : 1
* des : 这是一条有内涵的新闻1111
*/ private int id;
private String title;
private int type;
private String des; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getDes() {
return des;
} public void setDes(String des) {
this.des = des;
} @Override
public String toString() {
return "DataBean{" +
"id=" + id +
", title='" + title + '\'' +
", type=" + type +
", des='" + des + '\'' +
'}';
}
} }
4)接下来就是使用gson解析啦
/**
* gson解析json数据
*
* @param s
*/
private void gsonUtil(String s) {
//创建一个gson对象
Gson gson = new Gson();
//解析json数据
NewsInfo newsInfo = gson.fromJson(s, NewsInfo.class); String header = newsInfo.getHeader();
int retcode = newsInfo.getRetcode(); Log.i(TAG, "retcode=" + retcode + "----------header=" + header); //得到data数据的集合
List<NewsInfo.DataBean> data = newsInfo.getData(); Log.i(TAG, "data------->" + data.toString());
}
打印结果
retcode=----------header=http://192.168.126.26:8080/news/a.jpg data------->[DataBean{id=, title='新闻', type=, des='这是一条有内涵的新闻1111'},
DataBean{id=, title='专题', type=, des='这是一条有内涵的新闻222222'},
DataBean{id=, title='组图2', type=, des='这是一条有内涵的新闻333333'},
DataBean{id=, title='组图4', type=, des='这是一条有内涵的新闻333333'},
DataBean{id=, title='组图5', type=, des='这是一条有内涵的新闻333333'},
DataBean{id=, title='组图6', type=, des='这是一条有内涵的新闻ddddd33'},
DataBean{id=, title='组图7', type=, des='这是一条有内涵的新闻3ssss33333'},
DataBean{id=, title='组图8', type=, des='这是一条有内涵的新闻33dddd33333'},
DataBean{id=, title='互动', type=, des='这是一条有内涵的新闻444444'}]
最后贴上原始的json数据
{
"retcode": ,
"data": [
{
"id": ,
"title": "新闻",
"type": ,
"des":"这是一条有内涵的新闻1111"
},
{
"id": ,
"title": "专题",
"type": ,
"des":"这是一条有内涵的新闻222222"
},
{
"id": ,
"title": "组图2",
"type": ,
"des":"这是一条有内涵的新闻333333"
},
{
"id": ,
"title": "组图4",
"type": ,
"des":"这是一条有内涵的新闻333333"
},
{
"id": ,
"title": "组图5",
"type": ,
"des":"这是一条有内涵的新闻333333"
},
{
"id": ,
"title": "组图6",
"type": ,
"des":"这是一条有内涵的新闻ddddd33"
},
{
"id": ,
"title": "组图7",
"type": ,
"des":"这是一条有内涵的新闻3ssss33333"
},
{
"id": ,
"title": "组图8",
"type": ,
"des":"这是一条有内涵的新闻33dddd33333"
},
{
"id": ,
"title": "互动",
"type": ,
"des":"这是一条有内涵的新闻444444"
}
],
"header":"http://192.168.126.26:8080/news/a.jpg" }
好啦操作到此结束