微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

json2

接下来看一段复杂的json文本,要求用java代码解析当前的天气 和 未来三天的天气:

{

            "count": 1,"created": "2016-06-24T05:44:43Z","lang": "null","results":
            { "channel": { "units": { "distance": "km","pressure": "mb","speed": "km/h","temperature": "C" },"title": "Yahoo! Weather - Changsha,Hunan,CN","link": "http://us.rd.yahoo.com/dailynews/RSS/weather/Country__Country/*https://weather.yahoo.com/country/state/city-12686154/","description": "Yahoo! Weather for Changsha,"language": "en-us","lastBuildDate": "Fri,24 Jun 2016 01:44 PM CST","ttl": "60","location": { "city": "Changsha","country": "China","region": " Hunan" },"wind": { "chill": "88","direction": "293","speed": "22.53" },"atmosphere": { "humidity": "90","pressure": "33796.17","rising": "0","visibility": "24.94" },"astronomy": { "sunrise": "5:33 am","sunset": "7:28 pm" },"image": { "title": "Yahoo! Weather","width": "142","height": "18","link": "http://weather.yahoo.com","url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif" },"item": { "title": "Conditions for Changsha,CN at 01:00 PM CST","lat": "28.148609","long": "112.996727","pubDate": "Fri,24 Jun 2016 01:00 PM CST","condition": { "code": "47","date": "Fri,"temp": "30","text": "Scattered Thunderstorms" },"forecast": [ { "code": "4","date": "24 Jun 2016","day": "Fri","high": "31","low": "25","text": "Thunderstorms" },{ "code": "26","date": "25 Jun 2016","day": "Sat","high": "24","low": "22","text": "Cloudy" },{ "code": "47","date": "26 Jun 2016","day": "Sun","high": "28","low": "21","text": "Scattered Thunderstorms" },{ "code": "4","date": "27 Jun 2016","day": "Mon","high": "32","low": "23","date": "28 Jun 2016","day": "Tue","high": "33","date": "29 Jun 2016","day": "Wed","low": "27","date": "30 Jun 2016","day": "Thu","high": "29","date": "01 Jul 2016","high": "30","date": "02 Jul 2016","low": "26","date": "03 Jul 2016","text": "Thunderstorms" } ],"guid": { "isPermaLink": "false" } } } } }

可以看到 ,数据比较复杂,嵌套层数很多。有两种办法,一种是一层一层地找,另外一种是用json-path,JSONpath.read(json字符串,”$.第一层.第二层…”)
代码如下:

//解析json    
    public static void parseJson(String json) {
        //String t1="{'hi':'nihao'}";

        //这样一层一层地找很麻烦
        JSONObject jsonObject=new JSONObject().fromObject(json);
        //System.out.println(jsonObject);
        JSONObject j2=(JSONObject) jsonObject.get("results");
        //System.out.println(j2);
        //String count=jsonObject.getString("count");
        //System.out.println(count);
        JSONObject j3=(JSONObject) j2.get("channel");
        //System.out.println(j3); 
        JSONObject j4=(JSONObject) j3.get("item");
        //System.out.println(j4); 
        JSONObject j5=(JSONObject) j4.get("condition"); 
        //System.out.println(j5); 
        System.out.println("当前长沙市温度:"+j5.getInt("temp"));
        System.out.println("当前长沙市天气描述:"+j5.getString("text"));

        JSONArray jsonArray=j4.getJSONArray("forecast");
        //System.out.println(jsonArray);

        System.out.println("未来三天天气情况:");

        for(int i=1;i<=3;i++)
        {

            JSONObject j=jsonArray.getJSONObject(i);
            System.out.println(j.getString("date")+":最高温度"+j.getInt("high")+",最低温度:"+j.getInt("low")+",描述:"+j.getString("text"));
        }


        //用json-path更简单
        System.out.println("-------------json path-----------------");
        //JSONObject jObject=JsonPath.read(json,"$.results.channel.item.condition");返回类型需为linkedhashmap
        LinkedHashMap<String,Object> condition=JsonPath.read(json,"$.results.channel.item.condition");
        //$:根目录
        //System.out.println(condition);
        System.out.println("当前长沙市温度:"+condition.get("temp"));
        System.out.println("当前长沙市天气描述:"+condition.get("text"));
        //System.out.println(jObject);
        //JSONArray array=JsonPath.read(json,"$.results.channel.item.forecast");
        //System.out.println(JsonPath.parse(jsonObject));
        net.minidev.json.JSONArray array=JsonPath.read(json,"$.results.channel.item.forecast");
        //LinkedHashMap<String,Object> myjJsonObject=(LinkedHashMap<String,Object>) array.get(0);
        //System.out.println(myjJsonObject);
        //System.out.println(array);
        //LinkedHashMap<String,Object> item=JsonPath.read(json,"$.results.channel.item");
        //System.out.println(item);
        for(int i=1;i<=3;i++)
        {

            LinkedHashMap<String,Object> j=(LinkedHashMap<String,Object>) array.get(i);
            System.out.println(j.get("date")+":最高温度"+j.get("high")+",最低温度:"+j.get("low")+",描述:"+j.get("text"));
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐