zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    数据结构——霍夫曼树及题目场景应用
    算法——模式匹配
    深入理解Java虚拟机(十)——线程安全与锁优化
    深入理解Java虚拟机(九)——后端编译与优化
    算法——计算点集中共线最多点的直线
    算法——移掉K位数字使得数值最小
    算法—— n个骰子的点数
    Java并发编程的艺术(十二)——并发容器和框架
    算法——不用加减乘除符号运算加法
    5章-项目范围管理-day4
  • 原文地址:https://www.cnblogs.com/lxjhoney/p/6383958.html
Copyright © 2011-2022 走看看