json三层解析(数组解析)

里面多了数组,所以用到了JOSNArray

 1 package com.xykj.weather;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.net.MalformedURLException;
 7 import java.net.URL;
 8 
 9 import net.sf.json.JSONArray;
10 import net.sf.json.JSONObject;
11 
12 public class Weather {
13 
14     public static void main(String[] args) {
15         try {
16             URL url = new URL(
17                     "http://apicloud.mob.com/v1/weather/query?province=%E6%B9%96%E5%8D%97&key=520520test&city=%E6%B2%85%E9%99%B5");
18             try {
19                 BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
20                 StringBuffer sb = new StringBuffer();
21                 String st;
22                 while ((st = br.readLine()) != null) {
23                     sb.append(st);
24                 }
25                 System.out.println(sb.toString());
26                 JSONObject first = JSONObject.fromObject(sb.toString());
27                 System.out.println("============json解析第一层============");
28                 System.out.println("msg:" + first.getString("msg"));
29                 System.out.println("result:" + first.getString("result"));
30                 System.out.println("retCode:" + first.getString("retCode"));
31                 
32                 // JSONArray解析数组,如果不是数组就直接用JSONObject就可以了
33                 JSONArray result = JSONArray.fromObject(first.getString("result"));
34                 System.out.println("============json解析第二层============");
35                 for (int i = 0; i < result.size(); i++) {
36                     JSONObject second = result.getJSONObject(i);
37                     System.out.println("需要解析的:" + second);
38                     System.out.println("省:\t" + second.get("province"));
39                     System.out.println("市:\t" + second.get("city"));
40                     System.out.println("县:\t" + second.get("distrct"));
41                     System.out.println("日期:\t" + second.get("date"));
42                     System.out.println("空气:\t" + second.get("airCondition"));
43                     System.out.println("湿度:\t" + second.get("humidity"));
44                     System.out.println("污染指数:\t" + second.get("pollutionIndex"));
45                     System.out.println("天气:\t" + second.get("weather"));
46                     System.out.println("风:\t" + second.get("wind"));
47 
48                     System.out.println("============json解析第三层===========");
49                     JSONArray future = JSONArray.fromObject(second.get("future"));
50                     for (int j = 0; j < future.size(); j++) {
51                         JSONObject thirdly = future.getJSONObject(j);
52                         System.out.println("===========未来天气============");
53                         System.out.println("日期:" + thirdly.get("date"));
54                         System.out.println("白天:" + thirdly.get("dayTime"));
55                         System.out.println("夜晚:" + thirdly.get("night"));
56                         System.out.println("气温:" + thirdly.get("temperature"));
57                         System.out.println("风:" + thirdly.get("wind"));
58                     }
59                 }
60             } catch (IOException e) {
61                 // TODO Auto-generated catch block
62                 e.printStackTrace();
63             }
64         } catch (MalformedURLException e) {
65             e.printStackTrace();
66         }
67     }
68 
69 }
原文地址:https://www.cnblogs.com/lxjhoney/p/6383958.html