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

 package com.xykj.weather;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class Weather { public static void main(String[] args) {
try {
URL url = new URL(
"http://apicloud.mob.com/v1/weather/query?province=%E6%B9%96%E5%8D%97&key=520520test&city=%E6%B2%85%E9%99%B5");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer sb = new StringBuffer();
String st;
while ((st = br.readLine()) != null) {
sb.append(st);
}
System.out.println(sb.toString());
JSONObject first = JSONObject.fromObject(sb.toString());
System.out.println("============json解析第一层============");
System.out.println("msg:" + first.getString("msg"));
System.out.println("result:" + first.getString("result"));
System.out.println("retCode:" + first.getString("retCode")); // JSONArray解析数组,如果不是数组就直接用JSONObject就可以了
JSONArray result = JSONArray.fromObject(first.getString("result"));
System.out.println("============json解析第二层============");
for (int i = 0; i < result.size(); i++) {
JSONObject second = result.getJSONObject(i);
System.out.println("需要解析的:" + second);
System.out.println("省:\t" + second.get("province"));
System.out.println("市:\t" + second.get("city"));
System.out.println("县:\t" + second.get("distrct"));
System.out.println("日期:\t" + second.get("date"));
System.out.println("空气:\t" + second.get("airCondition"));
System.out.println("湿度:\t" + second.get("humidity"));
System.out.println("污染指数:\t" + second.get("pollutionIndex"));
System.out.println("天气:\t" + second.get("weather"));
System.out.println("风:\t" + second.get("wind")); System.out.println("============json解析第三层===========");
JSONArray future = JSONArray.fromObject(second.get("future"));
for (int j = 0; j < future.size(); j++) {
JSONObject thirdly = future.getJSONObject(j);
System.out.println("===========未来天气============");
System.out.println("日期:" + thirdly.get("date"));
System.out.println("白天:" + thirdly.get("dayTime"));
System.out.println("夜晚:" + thirdly.get("night"));
System.out.println("气温:" + thirdly.get("temperature"));
System.out.println("风:" + thirdly.get("wind"));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
} }

json三层解析(数组解析)的更多相关文章

  1. C# Newtonsoft.Json解析数组的小例子[转]

    https://blog.csdn.net/Sayesan/article/details/79756738 C# Newtonsoft.Json解析数组的小例子  http://www.cnblog ...

  2. jquery解析php通过ajax传过来的json二维数组对象

    ajax获得php传过来的json二维数组对象,jquery解析 php代码: <?php $news = array( '武汉'=>array(1,2,3), '广州'=>arra ...

  3. web service json 数组解析

     boolean workexpMark = true;     // 美发师工作经历json数组解析     org.json.JSONObject jsonObject = new org.j ...

  4. jmeter解析response里的json对象和数组

    1.解析提取json对象 2.解析提取json数组 注意,标红这里是从0开始计数 提取最后一个数组

  5. ***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)

    客户端的请求体中的数据:[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id& ...

  6. Java中json的构造和解析

    什么是 Json? JSON(JvaScript Object Notation)(官网网站:http://www.json.org/)是 一种轻量级的数据交换格式.  易于人阅读和编写.同时也易于机 ...

  7. 关于json解析中 解析多重json对象

    JSONObject rst = {"AIS-RST":"AIS-00000001","AIS-STATUS":"AIS-0000 ...

  8. iOS-数据持久化基础-JSON与XML数据解析

    解析的基本概念 所谓“解析”:从事先规定好的格式串中提取数据 解析的前提:提前约定好格式.数据提供方按照格式提供数据.数据获取方按照格式获取数据 iOS开发常见的解析:XML解析.JSON解析 一.X ...

  9. Gson解析数组多类型元素

    本文的出现是为了解决以下需求:使用Gson对json数组进行解析,但是数组里面元素的类型是多种的.数据如下: {"list":[{ "type":0, &quo ...

随机推荐

  1. 像感冒一样的contains error

    转自 http://blog.csdn.net/zhufuing/article/details/8135270          Android开发中的问题总是多种多样,今天我来总结一个浪费了我一个 ...

  2. Hibernate 之 Locking

    在我们业务实现的过程中,往往会有这样的需求:保证数据访问的排他性,也就是我正在访问的数据,别人不能够访问,或者不能对我的数据进行操作.面对这样的需求,就需要通过一种机制来保证这些数据在一定的操作过程中 ...

  3. python iterable 和list、dictionary的区别和联系

    1 为什么一些函数的参数指定要iterable object的,但是也可以传入list为参数? 因为list.dictionary都是iterable object. 在iterable object ...

  4. Koa2学习(二)async/await

    Koa2学习(二)async/await koa2中用到了大量的async/await语法,要学习koa2框架,首先要好好理解async/await语法. async/await顾名思义是一个异步等待 ...

  5. WinPE:创建 USB 可引导驱动器

    https://msdn.microsoft.com/zh-cn/library/windows/hardware/dn938386(v=vs.85).aspx

  6. JDBC访问数据库查询信息的步骤(硬编码格式)

    1 Class.forName()加载驱动 2 DriverManager获取Connect连接 3 创建Statement执行SQL语句 4 返回ResultSet查询结果 5释放资源 packag ...

  7. lucene 索引文件大小分布_tim

    Hi, I have index ~31G where27% of the index size is .fdt files (8.5G)20% - .fdx files (6.2G)37% - .f ...

  8. apple IOS的base64编解码

    <pre style="word-wrap: break-word; white-space: pre-wrap;">/* * Copyright (c) 2003 A ...

  9. Watir: 对浏览器的保存文件操作, 其实应用的是AutoIt脚本

    def save_file(filepath) ai =WIN32OLE.new("AutoItX3.Control") ai.WinWait("FileDownload ...

  10. bzoj 2005: [Noi2010]能量采集【莫比乌斯反演】

    注意到k=gcd(x,y)-1,所以答案是 \[ 2*(\sum_{i=1}^{n}\sum_{i=1}^{m}gcd(i,j))-n*m \] 去掉前面的乘和后面的减,用莫比乌斯反演来推,设n< ...