第一步注册:  compile 'com.google.code.gson:gson:2.6.2'

第二步初始化:

 Gson gson = new GsonBuilder()
.setLenient()// json宽松
.enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式
.serializeNulls() //智能null
.setPrettyPrinting()// 调教格式
.disableHtmlEscaping() //默认是GSON把HTML 转义的
.create();

1.简单json解析:

[{username:"arthinking",userId:"001"},{username:"Jason",userId:"002"}];

public class User {
private String username ;
private int userId ;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}

解析:

Type listType = new TypeToken<LinkedList<User>>(){}.getType();
Gson gson = new Gson();
LinkedList<User> users = gson.fromJson(jsonData, listType);
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getUsername());
System.out.println(user.getUserId());
}

复杂一点的json

  {
"data": {
"partnerteamlist": [
{
"pteamId": 72825,
"ptitle": "随摄影/共6套服装/准爸准妈共拍/免费肚画/底片全送。",
"pteamprice": 288
},
{
"pteamId": 72598,
"ptitle": "随摄影/拍摄200张/4本相册/品质拍摄/送全新婚纱。",
"pteamprice": 2888
},
{
"pteamId": 72613,
"ptitle": "随摄影/送全新婚纱/多外景拍摄/服装不限数量/绝无二次消费!",
"pteamprice": 3699
},
{
"pteamId": 72638,
"ptitle": "随摄影/服装不限数量/高品质拍摄260张/送全新婚纱。",
"pteamprice": 4299
},
{
"pteamId": 72716,
"ptitle": "随摄影/3组服装造型/内外景拍摄/完全透明消费!",
"pteamprice": 388
}
],
"liketeamlist": [
{
"lteamId": 65886,
"ltitle": "爱丽尔婚纱摄影/2本相册/6套服装造型/拍摄不限最低拍摄150张。",
"limage": "http://img.pztuan.com/upfile/team/2013/0712/201307120257551465.jpg",
"lteamprice": 518,
"lmarketprice": 3999
},
{
"lteamId": 57133,
"ltitle": "陶冶摄影/婚纱闺蜜/6组服装造型/拍摄不低于120张!",
"limage": "http://img.pztuan.com/upfile/team/2013/0628/201306281115249737.jpg",
"lteamprice": 580,
"lmarketprice": 3380
}
],
"feedbacks": {
"feedbacklist": [
{
"comment": "5分",
"createtime": "2014.07.08 13:38",
"score": 5,
"username": "l***2"
}
],
"totalcount": 1,
"totalscore": 5
}
},
"err": null,
"state": 1
}

解析model:

import java.util.List;  

    public class OtherDetail {  

        private int state;
private List<err> err;
private OtherDetail2 data; public int getState() {
return state;
} public void setState(int state) {
this.state = state;
} public List<err> getErr() {
return err;
} public void setErr(List<err> err) {
this.err = err;
} public OtherDetail2 getData() {
return data;
} public void setData(OtherDetail2 data) {
this.data = data;
} public class OtherDetail2 {
private List<partnerteamlist> partnerteamlist;
private List<liketeamlist> liketeamlist;
private List<feedbacks> feedbacks; public List<liketeamlist> getLiketeamlist() {
return liketeamlist;
} public void setLiketeamlist(List<liketeamlist> liketeamlist) {
this.liketeamlist = liketeamlist;
} public List<feedbacks> getFeedbacks() {
return feedbacks;
} public void setFeedbacks(List<feedbacks> feedbacks) {
this.feedbacks = feedbacks;
} public class partnerteamlist {
private int pteamId;
private String ptitle;
private Double pteamprice; public int getPteamId() {
return pteamId;
} public void setPteamId(int pteamId) {
this.pteamId = pteamId;
} public String getPtitle() {
return ptitle;
} public void setPtitle(String ptitle) {
this.ptitle = ptitle;
} public Double getPteamprice() {
return pteamprice;
} public void setPteamprice(Double pteamprice) {
this.pteamprice = pteamprice;
} } public class liketeamlist {
private int lteamId;
private String ltitle;
private String limage;
private Double lteamprice;
private Double lmarketprice; public int getLteamId() {
return lteamId;
} public void setLteamId(int lteamId) {
this.lteamId = lteamId;
} public String getLtitle() {
return ltitle;
} public void setLtitle(String ltitle) {
this.ltitle = ltitle;
} public String getLimage() {
return limage;
} public void setLimage(String limage) {
this.limage = limage;
} public Double getLteamprice() {
return lteamprice;
} public void setLteamprice(Double lteamprice) {
this.lteamprice = lteamprice;
} public Double getLmarketprice() {
return lmarketprice;
} public void setLmarketprice(Double lmarketprice) {
this.lmarketprice = lmarketprice;
}
} public class feedbacks {
private int totalcount;
private Double totalscore;
private List<feedbacklist> feedbacklist; public int getTotalcount() {
return totalcount;
} public void setTotalcount(int totalcount) {
this.totalcount = totalcount;
} public Double getTotalscore() {
return totalscore;
} public void setTotalscore(Double totalscore) {
this.totalscore = totalscore;
} public List<feedbacklist> getFeedbacklist() {
return feedbacklist;
} public void setFeedbacklist(List<feedbacklist> feedbacklist) {
this.feedbacklist = feedbacklist;
} public class feedbacklist {
private String username;
private String comment;
private String createtime;
private Double score; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getComment() {
return comment;
} public void setComment(String comment) {
this.comment = comment;
} public String getCreatetime() {
return createtime;
} public void setCreatetime(String createtime) {
this.createtime = createtime;
} public Double getScore() {
return score;
} public void setScore(Double score) {
this.score = score;
} }
} public List<partnerteamlist> getPartnerteamlist() {
return partnerteamlist;
} public void setPartnerteamlist(List<partnerteamlist> partnerteamlist) {
this.partnerteamlist = partnerteamlist;
}
} public class err {
private int code;
private String msg; public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
}

gson使用

 Gson gson = new Gson();
OtherDetail d = gson.fromJson(jsonString,Detail.class);//取值

android gson使用的更多相关文章

  1. 【转】Android Gson的使用

    Android Gson 2014 年 05 月 22 日 android 目前的客户端大都有和服务端进行交互,而数据的格式基本就是json了,于是在Android开发中就经常用到json解析,方便的 ...

  2. Android Gson的使用总结

    1.概念 Gson是谷歌发布的一个json解析框架 2.如何获取 github:https://github.com/google/gson android studio使用 compile 'com ...

  3. Android Gson 操作

    JSON序列化后的数据不带类名与名命空间,所以这两个服务端跟客户端可以不对应,需要保证字段对应即可 Asp.net MVC端 using System; using System.Collection ...

  4. Android Gson解析

    目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),解析速度最快的是Gson,下载地址:https://co ...

  5. Android Gson使用笔记

    最近在做一个java web service项目,需要用到jason,本人对java不是特别精通,于是开始搜索一些java平台的json类库. 发现了google的gson,因为之前对于protoco ...

  6. Android Gson深入分析

    眼下解析json有三种工具:org.json(Java经常使用的解析),fastjson(阿里巴巴project师开发的).Gson(Google官网出的).解析速度最快的是Gson,下载地址:htt ...

  7. Android Gson解析json详解

    目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),解析速度最快的是Gson,下载地址:https://co ...

  8. Android Gson解析复杂Json

    JSON原数据 {"total":1,"rows":[{"ID":1,"Title":"台州初级中学招收初一年 ...

  9. Android Gson解析json工具类封装

    package com.springSecurity.gson; import java.util.ArrayList; import java.util.List; import java.util ...

随机推荐

  1. 解决 openpyxl 垂直分页符和水平分页符同时添加的问题

    前言 十天前知乎上有人提问 python:openpyxl模块怎么给表格添加分页符?实现分页打印功能?,看到问题之后,我很快的给他了一个如何添加垂直分页符或水平分页符的示例,你以为问题就结束了?我是这 ...

  2. EasyTouch5初步用法和其中的一个Bug

    (一)配置部分:一.将预设体拖入场景中,我用的是下图这个预设体,因为既有摇杆又有按钮嘛,两个正好都能学习到 二.改变摇杆和按钮的外观,如下图所示,可以看出这个插件是用UGUI写的,改图片只需要改Ima ...

  3. 通过phoenix查看hbase中表的结构

    需求描述: 今天一个同事,问如何在phoenix中查询hbase中表的结构,在此记录下. 操作过程: 1.通过phoenix客户端连接到hbase数据库 [aiprd@host---- bin]$ . ...

  4. NHibernate实例

    1. 下载相关资源: 下载NHibernate.下载地址: http://nhforge.org/Default.aspx 下载微软Northwind示例数据库,下载地址:http://www.mic ...

  5. swift - 之TabBarController的用法

    TabBarController的使用,下面记录两种写法,代码如下: TabBarItem系统自带图标样式(System)介绍: Custom:自定义方式,配合Selected Image来自定义图标 ...

  6. 点击一个textView里的link导航至程序内可返回的自定义webView

    1,在AppDelegate.h里定义一个 id currentViewController; 在AppDelegate.m里 @implementation UIApplication (Priva ...

  7. OracleServiceORCL这个服务竟然不见了

    OracleServiceORCL这个服务竟然不见了,后数据库连接不成功,晕死,以前使用数据库还能看到,现在竟然不见了?Why?我猜测原因有二: ①:电脑已经装了Oracle数据库后又装了MySql数 ...

  8. JS面向对象编程学习

    学习目标:1.掌握JS中的类(原型对象)和对象.2.什么是成员变量和成员方法.3.掌握构造方法的使用.补充:关于双等号(==):1.如果等号两边都是字符串时,则比较内容是否相等2.如果等号两边是数字时 ...

  9. JS 添加js节点

    function AddScriptNode(src) { var N = document.createElement("script"); N.setAttribute(&qu ...

  10. 深入浅出Docker(二):Docker命令行探秘

    1. Docker命令行 Docker官方为了让用户快速了解Docker,提供了一个交互式教程,旨在帮助用户掌握Docker命令行的使用方法.但是由于Docker技术的快速发展,此交互式教程已经无法满 ...