package fastjson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/*
* 1.URLConnection 的使用
*
* 2.fastJson 的使用
*
*/
public class connAPI {
//xml格式数据
public static void xml() throws Exception{
//参数url化
String city = java.net.URLEncoder.encode("北京", "utf-8");
//拼地址
String apiUrl = String.format("https://www.sojson.com/open/api/weather/xml.shtml?city=%s",city);
//开始请求
URL url= new URL(apiUrl);
URLConnection open = url.openConnection();
InputStream input = open.getInputStream();
//这里转换为String,带上包名,怕你们引错包
String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");
//输出
System.out.println(result);
}

//json 格式
public static void json() throws Exception{
//参数url化
String city = java.net.URLEncoder.encode("北京", "utf-8");
//拼地址
String apiUrl = String.format("https://www.sojson.com/open/api/weather/json.shtml?city=%s",city);
//开始请求
URL url= new URL(apiUrl);
URLConnection open = url.openConnection();
InputStream input = open.getInputStream();
//这里转换为String,带上包名,怕你们引错包
String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");
//输出
System.out.println(result);
}
public static void fastjson(){
String neturl = "https://www.sojson.com/open/api/weather/json.shtml?city=%E5%8C%97%E4%BA%AC";
URL url;
try {
url = new URL(neturl);
StringBuffer document = new StringBuffer();
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line);
}
reader.close();
// {"date":"20180921","message":"Success !","status":200,"city":"北京","count":2,
// "data":{"shidu":"28%","pm25":13.0,"pm10":23.0,"quality":"优","wendu":"19","ganmao":"各类人群可自由活动",
// "yesterday":{"date":"20日星期四","sunrise":"05:58","high":"高温 27.0℃","low":"低温 16.0℃","sunset":"18:17","aqi":54.0,"fx":"南风","fl":"<3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},
// "forecast":[{"date":"21日星期五","sunrise":"05:59","high":"高温 25.0℃","low":"低温 14.0℃","sunset":"18:15","aqi":34.0,"fx":"西北风","fl":"3-4级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},
// {"date":"22日星期六","sunrise":"06:00","high":"高温 23.0℃","low":"低温 14.0℃","sunset":"18:13","aqi":40.0,"fx":"西北风","fl":"3-4级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},
// {"date":"23日星期日","sunrise":"06:01","high":"高温 23.0℃","low":"低温 12.0℃","sunset":"18:12","aqi":30.0,"fx":"北风","fl":"4-5级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},
// {"date":"24日星期一","sunrise":"06:02","high":"高温 22.0℃","low":"低温 11.0℃","sunset":"18:10","aqi":37.0,"fx":"北风","fl":"<3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},
// {"date":"25日星期二","sunrise":"06:03","high":"高温 23.0℃","low":"低温 13.0℃","sunset":"18:09","aqi":52.0,"fx":"西南风","fl":"<3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"}]}}

JSONObject jsonObject = JSON.parseObject(document.toString());
String date = jsonObject.getString("date");
String message = jsonObject.getString("message");
Integer status = jsonObject.getInteger("status");
String city = jsonObject.getString("city");
// 解析后面
String datas = jsonObject.getString("data");
JSONObject jb = JSON.parseObject(datas);
String shidu = jb.getString("shidu");
String wendu = jb.getString("wendu");
String ganmao = jb.getString("ganmao");
Integer pm25 = jb.getInteger("pm25");
Integer pm10 = jb.getInteger("pm10");
String quality = jb.getString("quality");

System.out.println(document);
System.out.println(date);
System.out.println(message);
System.out.println(status);
System.out.println(city);
System.out.println(datas);
System.out.println(shidu);
System.out.println(pm25);
System.out.println(pm10);
System.out.println(quality);
System.out.println(shidu);
System.out.println(wendu);
System.out.println(ganmao);

JSONArray forecast = jb.getJSONArray("forecast");
System.out.println(forecast);
for (int i = 0; i < forecast.size(); i++) {
JSONObject json = forecast.getJSONObject(i);
System.out
.println(json.getString("fl") + ":"
+ json.getString("fx") + ":"
+ json.getString("notice"));
}
String yesterday = jb.getString("yesterday");
JSONObject yjb = JSON.parseObject(yesterday);
System.out.println(yjb.toString());
String date2 = yjb.getString("date");
String sunrise = yjb.getString("sunrise");
System.out.println(date2);
System.out.println(sunrise);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//fastjson();
try {
//xml();
json();
} catch (Exception e) {
e.printStackTrace();
}
}
}

最新文章

  1. ABP(现代ASP.NET样板开发框架)系列之5、ABP启动配置
  2. Error Domain=ASIHTTPRequestErrorDomain Code=8 &quot;Failed to move file from&quot;xxx/xxx&quot;to&quot;xxx/xxx&quot;
  3. ubuntu 下安装boost库
  4. 火车站(codevs 2287)
  5. 在解决方案中所使用 NuGet 管理软件包依赖
  6. CCNU-线段树练习题-A-单点更新1
  7. Android面向HTTP协议发送post请求
  8. Mybatis学习总结(二)—使用接口实现数据的增删改查
  9. cf C. Bombs
  10. 如何以非 root 用户将应用绑定到 80 端口-ssh 篇
  11. selenium IDE中log的保存与查看方法
  12. yum源配置问题
  13. sass的基本使用
  14. Python3学习之路~6.5 私有属性和私有方法
  15. IOP知识点(3)-Modal.show
  16. [日常] Go语言圣经-错误,函数值习题
  17. 解决Matlab当中for循环运行慢的问题
  18. C#构造方法--实例化类时初始化的方法
  19. java基础学习总结——GUI编程(二)
  20. python笔记之循环控制

热门文章

  1. web资源持续更新----20150213
  2. DP入门练习
  3. linux定时任务执行php任务
  4. Cocos2D 添加 UIView
  5. 牛客网暑期ACM多校训练营(第五场) E room(最小费用最大流 , 最小权二分图匹配模板)
  6. 组队赛Day1第一场 GYM 101350 G - Snake Rana (容斥)
  7. Robotium测试没有源码的apk--需重签名apk
  8. express中间件的next()方法
  9. python基础-面向对象(类)
  10. DefaultActionInvocation 源码