json三层解析(数组解析)
2024-09-03 07:23:30
里面多了数组,所以用到了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();
}
} }
最新文章
- 【单页应用之通信机制】view之间应该如何通信
- middleware - bodyparser
- iOS navigationcontroller pop 回到上一层视图 如何刷新
- sizzle源码分析 (4)sizzle 技术总结及值得我们学习的地方
- POJ 2367 (裸拓扑排序)
- java coder的水平
- Android实现SharePreferences和AutoCompletedTextView
- 网络(一),libevent客户端部分
- Servlet &; JSP - Java Web 访问资源的路径问题
- 【BZOJ】1925: [Sdoi2010]地精部落 DP+滚动数组
- 【JS模式】单体模式
- MySQL数据库入门笔记
- nginx系列14:对HTTP协议的反向代理proxy模块
- Android Studio 合并分支代码到主干的操作总结
- http之理解304
- BZOJ3536 : [Usaco2014 Open]Cow Optics
- Linux之文件(目录)默认权限、特殊权限与隐藏权限
- Linux文件权限属性后面有个点
- django 静态文件
- 解决UITableView上的cell的重用
热门文章
- [IT学习]学习Python过程需要记忆的一些坑
- ABAP ODATA接口开发
- 初识JVM虚拟机
- YTU 2893: F--Mark的双薪
- YTU 2800: 逗逗泡泡的保密电文
- win10解决vc++6.0不兼容问题方法
- ubuntu 12.04上安装QQ2013(转载)
- Python机器学习算法 — 关联规则(Apriori、FP-growth)
- Linux 常用命令十二 top
- bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛【Floyd】