json三层解析(数组解析)
里面多了数组,所以用到了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三层解析(数组解析)的更多相关文章
- C# Newtonsoft.Json解析数组的小例子[转]
https://blog.csdn.net/Sayesan/article/details/79756738 C# Newtonsoft.Json解析数组的小例子 http://www.cnblog ...
- jquery解析php通过ajax传过来的json二维数组对象
ajax获得php传过来的json二维数组对象,jquery解析 php代码: <?php $news = array( '武汉'=>array(1,2,3), '广州'=>arra ...
- web service json 数组解析
boolean workexpMark = true; // 美发师工作经历json数组解析 org.json.JSONObject jsonObject = new org.j ...
- jmeter解析response里的json对象和数组
1.解析提取json对象 2.解析提取json数组 注意,标红这里是从0开始计数 提取最后一个数组
- ***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)
客户端的请求体中的数据:[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id& ...
- Java中json的构造和解析
什么是 Json? JSON(JvaScript Object Notation)(官网网站:http://www.json.org/)是 一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机 ...
- 关于json解析中 解析多重json对象
JSONObject rst = {"AIS-RST":"AIS-00000001","AIS-STATUS":"AIS-0000 ...
- iOS-数据持久化基础-JSON与XML数据解析
解析的基本概念 所谓“解析”:从事先规定好的格式串中提取数据 解析的前提:提前约定好格式.数据提供方按照格式提供数据.数据获取方按照格式获取数据 iOS开发常见的解析:XML解析.JSON解析 一.X ...
- Gson解析数组多类型元素
本文的出现是为了解决以下需求:使用Gson对json数组进行解析,但是数组里面元素的类型是多种的.数据如下: {"list":[{ "type":0, &quo ...
随机推荐
- python学习第一
#python学习day1#一.变量#变量命名规范:#驼峰命名法:AgeOfPlane#下划线命名(推荐):age_of_plane#变量格式同C/C++#注意:变量不以中文命名:变量不宜过长:变量因 ...
- Hibernate中Criteria的完整用法?
http://www.cnblogs.com/mabaishui/archive/2009/10/16/1584510.html
- C控制台密码输入:输入一个字符显示一个星号
要在c控制台中输入一个字符显示一个星号, 则不能用"stdio.h'提供的库函数,因为它们都是带回显的,比如getchar() getchar()用来接收输入的字符串,输入一个字符就回显一个 ...
- Common non-standard response fields
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#cite_note-52 Common non-standard response f ...
- java中abstract和interface的區別(轉)
(一)概述 在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机制的存 在,才赋予了Java强大的 面向对象能力.abstract ...
- YTU 2811: 打鱼还是晒网
2811: 打鱼还是晒网 时间限制: 1 Sec 内存限制: 128 MB 提交: 192 解决: 150 题目描述 中国有句俗话"三天打鱼,两天晒网".小王从2000年的1月 ...
- RecyclerView的基本用法
RecyclerView 是一个增强版的ListView,不仅可以实现和ListView同样的效果,还优化了ListView中存在的各种不足之处 ResyslerView 能够实现横向滚动,这是Lis ...
- 一步一步学Silverlight 2系列(2):基本控件
述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- URAL2104. Game with a Strip(博弈)
There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2n, n squares ...
- Java-Runoob-高级教程-实例-字符串:11. Java 实例 - 字符串性能比较测试
ylbtech-Java-Runoob-高级教程-实例-字符串:11. Java 实例 - 字符串性能比较测试 1.返回顶部 1. Java 实例 - 字符串性能比较测试 Java 实例 以下实例演 ...