--> Gson解析jar包:  链接:http://pan.baidu.com/s/1slCeq77 密码:f9ig

--> 官方Json解析工具类:

 package com.dragon.java.jsonwebdata;

 import java.util.ArrayList;
import java.util.List; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; /**
* Json解析的工具类
*
* @author Auser
*
*/
public class JsonUtil {
/**
* 官方解析方法 --> 不管多复杂都可以解析,但是非常复杂!!!
*
* @param json
*/
public static void jsonParse(String json) {
List<Detail> details = new ArrayList<>();
try {
JSONObject object = new JSONObject(json);
if (object.getString("status").equals("000000")) {
if (object.getString("desc").equals("null")) {
details = JSONArray(details, object);
for (Detail detail : details) {
System.out.println(detail);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} /**
* Json数组
*
* @param details
* @param object
* @return List<Detail>
* @throws JSONException
*/
private static List<Detail> JSONArray(List<Detail> details,
JSONObject object) throws JSONException {
JSONArray array = object.getJSONArray("detail");
for (int i = 0; i < array.length(); i++) {
JSONObject object2 = array.getJSONObject(i);
int id = object2.getInt("id");
int xhid = object2.getInt("xhid");
String author = object2.getString("author");
String content = object2.getString("content");
String picUrl = object2.getString("picUrl");
String status = object2.getString("status");
Detail detail = new Detail(id, xhid, author, content, picUrl,
status);
details.add(detail);
}
return details;
}
}

--> Gson解析工具类:

--> 需要一个Data类 包括:

private String status;
private String desc;
private List<Detail> detail;

且必须有无参构造方法和属性的getter、setter方法

 package com.dragon.java.gsonwebdata;

 import java.util.List;

 import com.dragon.java.jsonwebdata.Detail;
import com.google.gson.Gson; /*
* Gson解析的工具类
*/
public class GsonUtil { /**
* Gson解析
*
* @param json
*/
public static void gsonParse(String json) {
Gson gson = new Gson();
Info info = gson.fromJson(json, Info.class);
List<Detail> detail = info.getDetail();
if (info.getStatus().equals("000000")) {
if (info.getDesc() == null) {
for (Detail detail2 : detail) {
System.out.println(detail2);
}
}
}
}
}

--> 有Gson解析谁还用官方的解析方法啊...

Java-->Gson解析相较于Json的更多相关文章

  1. Android利用Gson解析嵌套多层的Json

    参考:http://www.cnblogs.com/jxgxy/p/3677256.html 比如我们要解析一个下面这种的Json: String json = {"a":&quo ...

  2. java后台解析前端传来的json

    @RequestMapping(value = {"save"}) @ResponseBody public Result save(TBaseInterventionPlan m ...

  3. Gson解析list类型的json串

    Gson gson = new Gson(); Type type = new TypeToken<List<Object>>() {}.getType(); List< ...

  4. 通过Gson解析Json数据

    Json是一种数据格式,便于数据传输.存储.交换:Gson是一种组件库,可以把java对象数据转换成json数据格式. gson.jar的下载地址:http://search.maven.org/#s ...

  5. Gson解析json数据的案例一

    转自:http://blog.csdn.net/l331258747/article/details/51547338: Android利用Gson解析嵌套多层的Json 首先先讲一个比较简单点的例子 ...

  6. 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题

    1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...

  7. Gson解析:java.lang.IllegalArgumentException: declares multiple JSON fields named status 问题的解决

    在一次写定义系统统一返回值的情况下,碰到了java.lang.IllegalArgumentException: declares multiple JSON fields named status这 ...

  8. JAVA使用Gson解析json数据,实例

    封装类Attribute: public class Attribute { private int id; private String name; private int age; public ...

  9. JSON格式之GSON解析

    JSON格式之GSON解析 最近在做websocket相关,项目需要JSON解析.相较之下感觉google的GSON解析不错. JAVA后台 Gson提供了fromJson()方法来实现从Json相关 ...

随机推荐

  1. Bootstrap_导航

    一.标签形tab导航 标签形导航,也称为选项卡导航. 标签形导航是通过“.nav-tabs”样式来实现.在制作标签形导航时需要在原导航“.nav”上追加此类名. <ul class=" ...

  2. C#的多态性

    参考网址:http://www.cnblogs.com/zhangkai2237/archive/2012/12/20/2826734.html 多态的定义:同一操作作用于不同的对象,可以有不同的解释 ...

  3. Claims Identity

    using System;using System.Collections.Generic;using System.Linq;using System.Security.Claims;using S ...

  4. sqlloader导出数据和导入数据

    分类: Oracle 忙了一天终于把sqlloader导出数据和导入数据弄清楚了,累死俺了... 这个总结主要分为三个大部分,第一部分(实例,主要分两步),第二部分(参数小总结),第三部分(完全参数总 ...

  5. CSS 超出隐藏问题

    .dropdown-navbar>li:last-child>a { border-bottom: 0 solid #DDD; border-top: 1px dotted transpa ...

  6. Java 序列化 反序列化 历史版本处理

    直接引用  http://www.cnblogs.com/xdp-gacl/p/3777987.html

  7. iOS开发 火星坐标转百度坐标

    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(latitude, longitude);//原始坐标 //转换 google地图.s ...

  8. Hbase的WAL在RegionServer基本调用过程

    版权声明:本文由熊训德原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/221 来源:腾云阁 https://www.qclo ...

  9. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  10. jquery打字机效果

    html代码 <div id="box"> <div id="content"> <div id="code" ...