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 ...
随机推荐
- [luoguP1516] 青蛙的约会(扩展欧几里得)
传送门 对于数论只会gcd的我,也要下定决心补数论了 列出方程 (x + t * m) % l = (y + t * n) % l 那么假设 这两个式子之间相差 num 个 l,即为 x + t * ...
- Self Centos + Windows server 2016
Set up by Derek: 2019-1-25 登陆个人物理机: license 60天Free , 如果过期,就在 VMware ESXI 6.5.0的黑屏界面去reset. https:/ ...
- Hackerrank manasa-and-combinatorics(数学推导)
题意:有n个字符A,2n个字符B,问你能用这3n个字母组成多少种字符串,使得组成的字符串所有前缀与后缀的B的数目都大于等于A的数目,对答案mod 99991 分析:类似卡特兰数 ans=总方案数-存在 ...
- W3School Memcached教程(安装/基本操作/高级操作/命令)
来自W3School的Memcached教程,基本上涵盖了从安装到状态监控的教程. 不过最全的应该是官方提供在GitHub上的Wiki教程,一切的标准都来自官方,参考:https://github.c ...
- Visual Studio 中的 .NET Framework 类库
Visual Studio 中的 .NET Framework 类库 .NET Framework 类库由命名空间组成.每个命名空间都包含可在程序中使用的类型:类.结构.枚举.委托和接口. 当您在 V ...
- Linux经常使用命令-文件搜索命令-文件搜索命令find
命令名称:find 命令所在路径:/bin/find 语法:find [搜索范围] [匹配条件] 功能描写叙述:文件搜索 演示样例 find /etc - name init 在文件夹/etc 中查找 ...
- ssh2项目整合 struts2.1+hibernate3.3+spring3 基于hibernate注解和struts2注解
项目文件夹结构例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveW9uZ3poaWFu/font/5a6L5L2T/fontsize/400/fi ...
- J2EE肌肉系统—四层模型
J2EE是基于JAVA技术的一种标准.为什么会有这种标准呢? 主要是在企业级应用开发其中有一些需求.比如数据库连接,邮件服务.事件处理等,都是一些通用模块. 而这些模块假设由开发者来开发.势必添加开发 ...
- jedis 2.7.2 jar
jedis 2.7.2 已经公布. 源码https://github.com/xetorthio/jedis/releases/tag/jedis-2.7.2 jar 下载地址 http://dow ...
- 【POJ 2777】 Count Color
[题目链接] http://poj.org/problem?id=2777 [算法] 线段树 [代码] #include <algorithm> #include <bitset&g ...