曾经我自课堂上讲解的json案例2015年9月底的时候通过这个模板可以获取新闻了或者其他,比如我们现在做的实训项目,新闻发布系统,这是用到聚合数据的天气预报数据,key需要自己申请我的不会给你的
package json_test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import net.sf.json.JSONObject;
/***
*
* @author 马琳的笔记本
* 测试网站地址:https://www.juhe.cn/Box/index/id/39
* 聚合数据天气预报JSon实例
* JSONObjecttest类的fromObject(String);
* 得到一个json
*
*/
public class JSONObjecttest {
public static void main(String[] args) {
String charset ="UTF-8";
String urlname="http://v.juhe.cn/weather/index?format=2&cityname=%E6%B5%8E%E5%AE%81&key=d********你的自己的key***56d7d5a406aca1f3ad3ab73372";//appkey
String jsonResult = get(urlname,charset);//得到JSON字符串
JSONObject object = JSONObject.fromObject(jsonResult);//转化为JSON类
System.out.println( object);
String result;
JSONObject obj=JSONObject.fromObject(jsonResult);
/*获取返回状态码*/
result=obj.getString("resultcode");
System.out.println("连接状况码:"+result);
/*如果状态码是200说明返回数据成功*/
if(result!=null&&result.equals("200")){
result=obj.getString("result");
System.out.println(result);
//此时result中数据有多个key,可以对其key进行遍历,得到对个属性
obj=JSONObject.fromObject(result);
System.out.println(obj);
//今日温度对应的key是today
result=obj.getString("today");
System.out.println(result);
obj=JSONObject.fromObject(result);
System.out.println(obj);
//今日温度对应当key是temperature
System.out.println("对应城市:"+obj.getString("city"));
System.out.println("今日温度:"+obj.getString("temperature"));
System.out.println("天气更新于"+obj.getString("date_y"));
System.out.println("今日是"+obj.getString("week"));
System.out.println("今日天气:"+obj.getString("weather"));
System.out.println("今日风向;"+obj.getString("wind"));
System.out.println("洗澡指数:"+obj.getString("dressing_index"));
System.out.println("洗澡建议:"+obj.getString("dressing_advice"));
System.out.println("太阳紫外线强度:"+obj.getString("uv_index"));
System.out.println("游泳指数:"+obj.getString("wash_index"));
System.out.println("出外旅游指数:"+obj.getString("travel_index"));
System.out.println("晨练建议:"+obj.getString("exercise_index"));
}
}
/**
*
* @param urlAll:请求接口
* @param charset:字符编码
* @return 返回json结果
*/
public static String get(String urlAll,String charset){
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/29.0.1547.66 Safari/537.36";//模拟浏览器
try {
URL url = new URL(urlAll);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(30000);
connection.setConnectTimeout(30000);
connection.setRequestProperty("User-agent",userAgent);
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(
is,charset));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
/***
* 运行结果:
* {"resultcode":"200","reason":"successed!","result":{"sk":{"temp":"19","wind_direction":"东北风","wind_strength":"1级","humidity":"83%","time":"07:40"},"today":{"temperature":"17℃~26℃","weather":"阴转多云","weather_id":{"fa":"02","fb":"01"},"wind":"东风微风","week":"星期二","city":"济宁","date_y":"2015年09月15日","dressing_index":"舒适","dressing_advice":"建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。","uv_index":"弱","comfort_index":"","wash_index":"较适宜","travel_index":"适宜","exercise_index":"适宜","drying_index":""},"future":[{"temperature":"17℃~26℃","date":"20150915"},{"temperature":"17℃~27℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"东南风微风","week":"星期三","date":"20150916"},{"temperature":"18℃~27℃","weather":"晴转多云","week":"星期四","date":"20150917"},"weather":"阴转晴","wind":"微风","week":"星期五","date":"20150918"},{"temperature":"17℃~29℃","week":"星期六","date":"20150919"},{"temperature":"18℃~30℃","week":"星期日","date":"20150920"},{"temperature":"18℃~29℃","week":"星期一","date":"20150921"}]},"error_code":0}
连接状况码:200
{"sk":{"temp":"19","date":"20150921"}]}
{"sk":{"temp":"19","date":"20150921"}]}
{"temperature":"17℃~26℃","drying_index":""}
{"temperature":"17℃~26℃","drying_index":""}
对应城市:济宁
今日温度:17℃~26℃
天气更新于2015年09月15日
今日是星期二
今日天气:阴转多云
今日风向;东风微风
洗澡指数:舒适
洗澡建议:建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。
太阳紫外线强度:弱
游泳指数:较适宜
出外旅游指数:适宜
晨练建议:适宜
*
*
*
*
*/