码迷,mamicode.com
首页 > Web开发 > 详细

Gson解析复杂JSON对象

时间:2015-01-08 12:45:44      阅读:454      评论:0      收藏:0      [点我收藏+]

标签:

例如以下格式JSON:

技术分享

建立对应的Java对象,注意内部类要定义成静态的

public class HResult {
    
    public String total;
    public String records;
    public String page;
    
    public List<HCell> rows;
    
    static class HCell{
        public List<String> cell;
        public String id;
    }
    
    
}

使用Gson进行解析

public static void main(String[] args) {
        String json = "{\"total\":\"1\",\"rows\":[{\"cell\":[\"c1\",\"c2\",\"没有检索到相关历史信息,请点击[我要留言]\",\"c4\",\"c5\",\"c6\",\"c7\"],\"id\":\"999999999999999999999999999999\"}],\"records\":\"0\",\"page\":\"1\"}";
        
        Gson gson = new Gson();
        
        HResult hResult = gson.fromJson(json, new TypeToken<HResult>(){}.getType());
        
        System.out.println("total:"+hResult.total);
        System.out.println("records:"+hResult.records);
        System.out.println("page:"+hResult.page);
        
        List<HResult.HCell> list = hResult.rows;
        for(HResult.HCell row:list){
            System.out.println("cell:"+row.cell);
            System.out.println("id:"+row.id);
        }
    }

运行结果:

run:
total:1
records:0
page:1
cell:[c1, c2, 没有检索到相关历史信息,请点击[我要留言], c4, c5, c6, c7]
id:999999999999999999999999999999
成功构建 (总时间: 0 秒)

 

Gson解析复杂JSON对象

标签:

原文地址:http://www.cnblogs.com/yshyee/p/4210313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!