Json解析与Gson解析
本文主要介绍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" }
好啦操作到此结束
Json解析与Gson解析的更多相关文章
- JSON格式之GSON解析
JSON格式之GSON解析 最近在做websocket相关,项目需要JSON解析.相较之下感觉google的GSON解析不错. JAVA后台 Gson提供了fromJson()方法来实现从Json相关 ...
- Android解析Json数据之Gson解析
Gson是谷歌官方提供的解析json数据的工具类.json数据的解析能够使用JSONObject和JSONArray配合使用解析数据,可是这样的原始的方法对于小数据的解析还是有作用的,可是陪到了复杂数 ...
- 安卓数据解析之 fastjson 的解析以及Gson解析
在安卓开发过程中的.我们经常使用的数据传递是以json格式传递.安卓 亲爹提供了我们Gson解析工具.点击下载Gson.jar 阿里巴巴FastJson是一个Json处理工具包,包含"序列化 ...
- 定位和xml解析和gson解析加上拉加载,下拉刷新
这里的上拉加载,下拉刷新用到是依赖包 Mainactivity,xml解析和定位 package com.exmple.autolayout; import java.util.List; impor ...
- JSON三种数据解析方法(转)
原 JSON三种数据解析方法 2018年01月15日 13:05:01 zhoujiang2012 阅读数:7896 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- Gson解析空字符串异常的处理
面对一些不规范的json,我们的gson解析经常会抛出各种异常导致app崩溃,这里可以采取一些措施来避免. 我们期望在后台返回的json异常时,也能解析成功,空值对应的转换为默认值,如:newsId= ...
- Android中的应用——谷歌官方Json分析工具Gson使用
一个.Gson基本介绍 Gson(又称Google Gson)是Google公司公布的一个开放源码的Java库.主要用途为串行化Java对象为JSON字符串,或反串行化JSON字符串成Java对象. ...
- 使用Gson解析json
前边的博客说过将json解析成java的方法,使用的是 这几个jar包,但是在解析时层遇到一个问题,就是在将时间字符串转换为java的Timestamp对象时会抛出异常,这个问题一直放在哪里没有去解决 ...
- 大话JSON之Gson解析JSON
(三)解析Json数组(多条Json数据) 比如有如下Json数据: [{'name':'John', 'grade':[{'course':'English','score':100},{'cour ...
随机推荐
- [luoguP1029] 最大公约数和最小公倍数问题(数论)
传送门 一.暴力枚举(加了点优化) #include <cstdio> int x, y, ans; inline int gcd(int x, int y) { return !y ? ...
- java 源码分析2 -List
1.是一个接口,继承了Collection,提供了size(),isEmpty(),contanis(),iterator(),toArray(),clear()等方法 2.分析常用的ArrayLis ...
- [bzoj1597][USACO2008]土地购买(DP斜率优化/四边形优化)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1597 分析: 1.先可以把被包含的土地可以去掉,这些土地的长宽肯定都是不会用的,具体先 ...
- Ubuntu 16.04安装FTP客户端filezilla
1.安装: sudo apt-get install filezilla 参考: http://os.51cto.com/art/201103/247564.htm
- LVS 源代码分析
http://blog.chinaunix.net/uid/11207493.html http://zh.linuxvirtualserver.org/blog/3309
- Unity3D Asset文件导出3DMax 可编辑格式
本文章由cartzhang编写,转载请注明出处. 全部权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/60878354 作者:car ...
- 1.3-动态路由协议EIGRP
EIGRP(Enhanced IGRP) EIGRP的特点: IGRP/EIGRP都是CISCO的私有协议. 1:是唯一的一种LS/DV的混合协议. 2:Rapid convergence EIGRP ...
- Android实现微博分享及其注意事项
在前面我写了两篇关于QQ和微信的分享(http://blog.csdn.net/dawanganban/article/details/42015249)(http://blog.csdn.net/d ...
- R语言学习笔记-Error in ts(x):对象不是矩阵问题解决
1.问题 在对时间序列进行拟合操作时,发生:Error in ts(x):对象不是矩阵的错误,而直接在arima()函数中使用时没有问题的. > sample<-c2 > sampl ...
- 在IIS上搭建WebSocket服务器(二)
服务器端代码编写 1.新建一个ASP.net Web MVC5项目 2.新建一个“一般处理程序” 3.Handler1.ashx代码如下: using System; using System.Col ...