美文网首页Android开发网络请求
Android中使用Gson解析稍复杂的JSON数据

Android中使用Gson解析稍复杂的JSON数据

作者: Aspirinrin | 来源:发表于2014-10-07 17:34 被阅读5148次

    一、Json简介

    JSON官网(中文版):http://www.json.org/json-zh.html

    JSON(JavaScript Object Notation)是一种轻量级(轻量级?简单、易操作、快捷)的数据交换格式。主要目的就是给出一套通用的数据格式,大家按照这种格式定义自己的数据,方便数据的交换。特点是(相对来说)易于人阅读和编写,易于机器解析和生成

    Rules:

    1.“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。

    2.值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)

    JSON可以有以下格式:

    1.对象是一个无序的“ ‘名称/值’ 对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。

    object的  名称 /值  结构

    2.数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。

     “值的类型”可以是哪些数据类型

    以上是最基本的json知识,想深入了解的,请移步官网

    下面举个栗子给大家尝尝:

    {

    "type":"forecast1d",

    "weatherinfo": [

    {

    "city": "北京",

    "cityid": "1",

    "temp1": "22℃",

    "temp2": "10℃",

    "weather": "晴",

    "ptime": "11:00"

    },

    {

    "city": "上海",

    "cityid": "2",

    "temp1": "24℃",

    "temp2": "12℃",

    "weather": "晴",

    "ptime": "11:00"

    }

    ]

    }

    栗子好难看,上截图(截图太小看不清...戳这里看大图<-- !):

     一个简单的JSON串

    NOTE:左侧为JSON字符串,右侧为解析结构,方便查看。

    福利:截图是我在一个在线JSON Editor上截的,体验一下-->JSON Editor,很好用推荐给大家。

    二、使用Gson在Android中解析Json

    认清了JSON,就要解析它。

    你可以使用的JSON库:

    JSONObject(源自Android官方)、

    GSon(源自Google)、

    Jackson(第三方开源库)、

    FastJSON(第三方开源库)、

    等。。。

    本篇文章使用Gson解析JSON,Gson地址:http://code.google.com/p/google-gson/

    google-gson 

    无法下载?百度云分享一下http://pan.baidu.com/s/1kTur5xd,提取密码:5oae

    根据JSON串的结构定义一个类(这里我们把这个类叫Result),我们直接把得到的JSON串解析成这个类。class Result定义如下:

    import java.util.List;

    public class Result {

    public String type;

    public List

    weatherinfo;

    public static class Info {

    public String city;

    public String cityid;

    public String temp1;

    public String temp2;

    public String weather;

    public String ptime;

    }

    }

    class Result

    定义好了待解析成的class之后,接下来使用Gson解析JSON串就可以了:

    Gson gson = new Gson();

    Result r = gson.fromJson(jsonData, Result.class);

    So easy!

    难点:1.如何定义这个待解析成的类?其实很简单,看到JSON结构里面有{ }你就定义一个类,看到[ ]你就定义一个List即可,最后只剩下最简单的如String、int等基本类型直接定义就好。2.内部嵌套的类,请使用public static class className { }。3.类内部的属性名,必须与JSON串里面的Key名称保持一致。这三点请自行对照我们上面举的Result的栗子,都有对应。

    相关文章

      网友评论

      • 后端沉思录::"100","code_n":"101","txt_d":"晴","txt_n":"多云"},
        "date":"2016-03-24","hum":"10","pcpn":"0.0","pop":"0","pres":"1029","tmp":{"max":"16","min":"3"},"vis":"10","wind":{"deg":"285","dir":"无持续风向","sc":"微风","spd":"4"}
        }
        ],

        "hourly_forecast":[
        {"date":"2016-03-18 10:00","hum":"22","pop":"0","pres":"1014","tmp":"18","wind":{"deg":"130","dir":"东南风","sc":"微风","spd":"13"}},
        {"date":"2016-03-18 13:00","hum":"12","pop":"0","pres":"1014","tmp":"22","wind":{"deg":"39","dir":"东北风","sc":"微风","spd":"15"}},
        {"date":"2016-03-18 16:00","hum":"12","pop":"0","pres":"1014","tmp":"22","wind":{"deg":"80","dir":"东风","sc":"3-4","spd":"17"}},
        {"date":"2016-03-18 19:00","hum":"19","pop":"0","pres":"1016","tmp":"18","wind":{"deg":"75","dir":"东北风","sc":"微风","spd":"14"}},
        {"date":"2016-03-18 22:00","hum":"27","pop":"0","pres":"1019","tmp":"14","wind":{"deg":"40","dir":"东北风","sc":"微风","spd":"11"}}
        ],

        "now":
        {
        "cond":{
        "code":"502",
        "txt":"霾"},
        "fl":"6",
        "hum":"45",
        "pcpn":"0",
        "pres":"1011",
        "tmp":"13",
        "vis":"3",
        "wind":{"deg":"350","dir":"东北风","sc":"4-5","spd":"18"}
        },


        "status":"ok",

        "suggestion":
        {
        "comf":{"brf":"舒适","txt":"白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。"},
        "cw":{"brf":"不宜","txt":"不宜洗车,未来24小时内有霾,如果在此期间洗车,会弄脏您的爱车。"},
        "drsg":{"brf":"较冷","txt":"建议着大衣、呢外套加毛衣、卫衣等服装。体弱者宜着厚外套、厚毛衣。因昼夜温差较大,注意增减衣服。"},
        "flu":{"brf":"易发","txt":"昼夜温差大,且空气湿度较大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},
        "sport":{"brf":"较不宜","txt":"有扬沙或浮尘,建议适当停止户外运动,选择在室内进行运动,以避免吸入更多沙尘,有损健康。"},
        "trav":{"brf":"较不宜","txt":"空气质量差,不适宜旅游"},
        "uv":{"brf":"最弱","txt":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}
        }


        }
        ]
        }
      • 后端沉思录:{"HeWeather data service 3.0":
        [{"aqi":
        {"city":
        {"aqi":"378","co":"2","no2":"96","o3":"19","pm10":"259","pm25":"327",
        "qlty":"严重污染","so2":"23"
        }
        },"basic":{
        "city":"北京","cnty":"中国","id":"CN101010100","lat":"39.904000","lon":"116.391000",
        "update":{"loc":"2016-03-18 09:48","utc":"2016-03-18 01:48"}
        },
        "daily_forecast":[
        {"astro":{"sr":"06:20","ss":"18:24"},
        "cond":{"code_d":"502","code_n":"101","txt_d":"霾","txt_n":"多云"},
        "date":"2016-03-18","hum":"11","pcpn":"0.0","pop":"0","pres":"1013","tmp":{"max":"21","min":"5"},"vis":"10","wind":{"deg":"47","dir":"无持续风向","sc":"微风","spd":"5"}},{"astro":{"sr":"06:18","ss":"18:25"},"cond":{"code_d":"101","code_n":"104","txt_d":"多云","txt_n":"阴"},
        "date":"2016-03-19","hum":"22","pcpn":"0.0","pop":"0","pres":"1023","tmp":{"max":"15","min":"4"},"vis":"10","wind":{"deg":"176","dir":"无持续风向","sc":"微风","spd":"2"}},{"astro":{"sr":"06:17","ss":"18:26"},"cond":{"code_d":"104","code_n":"101","txt_d":"阴","txt_n":"多云"},
        "date":"2016-03-20","hum":"24","pcpn":"0.0","pop":"0","pres":"1027","tmp":{"max":"16","min":"4"},"vis":"10","wind":{"deg":"159","dir":"无持续风向","sc":"微风","spd":"3"}},{"astro":{"sr":"06:15","ss":"18:27"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},
        "date":"2016-03-21","hum":"21","pcpn":"0.0","pop":"12","pres":"1021","tmp":{"max":"17","min":"5"},"vis":"10","wind":{"deg":"209","dir":"无持续风向","sc":"微风","spd":"0"}},{"astro":{"sr":"06:13","ss":"18:28"},"cond":{"code_d":"101","code_n":"101","txt_d":"多云","txt_n":"多云"},
        "date":"2016-03-22","hum":"27","pcpn":"0.0","pop":"46","pres":"1024","tmp":{"max":"18","min":"6"},"vis":"10","wind":{"deg":"124","dir":"无持续风向","sc":"微风","spd":"6"}},{"astro":{"sr":"06:12","ss":"18:29"},"cond":{"code_d":"101","code_n":"100","txt_d":"多云","txt_n":"晴"},
        "date":"2016-03-23","hum":"10","pcpn":"0.0","pop":"34","pres":"1031","tmp":{"max":"15","min":"4"},"vis":"10","wind":{"deg":"336","dir":"无持续风向","sc":"微风","spd":"2"}},{"astro":{"sr":"06:10","ss":"18:30"},"cond":{"code_d"
        后端沉思录:@Aspirinrin 我是想问,遇到上面的复杂的json,如何解析
        Aspirinrin:@diy_os 有什么问题吗?
        后端沉思录:@diy_os 请问解析过这样的数据么
      • 刻舟求剑KJ:可以请问一下那Main中的解析应该怎么写呢?
        Aspirinrin:@刻舟求剑KJ 请直接看文章最后的难点部分
        刻舟求剑KJ:@Aspirinrin 但是我解析不到类中的数组或者类中的类里的object,不知道是哪里写错了,您可以给我个邮箱或者联系方式吗
        Aspirinrin:@刻舟求剑KJ 无论在哪里使用 都是先创建一个类 然后使用gson的fromJson方法

      本文标题:Android中使用Gson解析稍复杂的JSON数据

      本文链接:https://www.haomeiwen.com/subject/juxptttx.html