里面多了数组,所以用到了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. web 开发之js---ajax 中的两种返回状态 xmlhttp.status和 xmlhttp.readyState

    (1)xmlhttp.status xmlHttp.status的值(HTTP状态表)0**:未被始化 1**:请求收到,继续处理 2**:操作成功收到,分析.接受 3**:完成此请求必须进一步处理 ...

  2. Node.js 101(2): Promise and async

    --原文地址:http://blog.chrisyip.im/nodejs-101-package-promise-and-async 先回想一下 Sagase 的项目结构: lib/ cli.js ...

  3. assert的作用是什么

    assert()宏是用于保证满足某个特定条件,用法是: assert(表达式); 如果表达式的值为假,整个程序将退出,并输出一条错误信息.如果表达式的值为真则继续执行后面的语句. 使用这个宏前需要包含 ...

  4. 关于warning: Clock skew detected. Your build may be incomplete. 的解决方法【转】

    本文转载自:http://blog.csdn.net/jeesenzhang/article/details/40300127 今天发现电脑的系统时间不正确,因此将时钟进行了修改,回头编译Linux ...

  5. HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...

  6. 使用C#开发HTTP服务器系列之实现Get和Post

     各位朋友大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是http://qinyuanpei.com.在我们这个Web服务器有了一个基本的门面以后,我们是时候来用它做点实际的事情了.还记得我们 ...

  7. Java-Runoob-高级教程-实例-字符串:14. Java 实例 - 连接字符串

    ylbtech-Java-Runoob-高级教程-实例-字符串:14. Java 实例 - 连接字符串 1.返回顶部 1. Java 实例 - 连接字符串  Java 实例 以下实例演示了通过 &qu ...

  8. body和html

    1 关于html和body的背景颜色的一些变现 当给body设置背景颜色时(html没有背景颜色),这时body被当做根节点被浏览器俘获,浏览器界面的背景颜色就为body的background颜色:当 ...

  9. 《Deep Learning Face Attributes in the Wild》论文笔记

    论文背景: IEEE International Conference on Computer Vision 2015 Ziwei Liu1, Ping Luo1, Xiaogang Wang2, X ...

  10. EF 连接MySql

    使用EntityFramework6连接MySql数据库(db first方式) http://www.cnblogs.com/24la/archive/2014/04/03/ef6-mysql.ht ...