Example of json parsing Sina Weather interface using java

Source: Internet
Author: User
Tags json stringbuffer

Example of json parsing Sina Weather interface using java


Json data returned by Sina Weather

[
    {
"CurrentCity": "Xiamen ",
"Pm25": "64 ",
"Index ":[
            {
"Title": "dressing ",
"Zs": "more comfortable ",
"Tipt": "Dressing index ",
"Des": "We recommend that you wear thin coats, denim shirts, and other clothing. Old and weak people should add appropriate clothing, and should wear jackets and thin sweaters. "
},
            {
"Title": "Car Wash ",
"Zs": "not suitable ",
"Tipt": "Car Wash index ",
"Des": "Do not wash your car. It may rain in the next 24 hours. If you wash your car during this period, the rain and the mud on the road may stain your car again. "
},
            {
"Title": "Tourism ",
"Zs": "suitable ",
"Tipt": "Tourism Index ",
"Des": "The weather is good, the temperature is good, and the weather is good. This weather is suitable for traveling, so you can enjoy the scenery of nature. "
},
            {
"Title": "Catch a Cold ",
"Zs": "Shaofa ",
"Tipt": "Cold index ",
"Des": "The weather conditions are suitable, and there is no obvious cooling process, resulting in a low chance of catching a cold. "
},
            {
"Title": "Sports ",
"Zs": "more suitable ",
"Tipt": "Motion Index ",
"Des": "cloudy day, suitable for various internal and external household sports. "
},
            {
"Title": "UV intensity ",
"Zs": "weakest ",
"Tipt": "UV intensity index ",
"Des": "It is a weather event with weak ultraviolet radiation and requires no special protection. If you are outdoors for a long time, it is recommended that you apply sunscreen between 8-12 SPF skin care products. "
            }
],
"Weather_data ":[
            {
"Date": "Monday, January 1, December 21 (real-time: 16 ℃ )",
"DayPictureUrl": "yun_qi_img/zhenyu.png ",
"Maid": "yun_qi_img/duoyun.png ",
"Weather": "Shower turns cloudy ",
"Wind": "Breeze ",
"Temperature": "19 ~ 15 ℃"
},
            {
"Date": "Tuesday ",
"DayPictureUrl": "yun_qi_img/duoyun.png ",
"Maid": "yun_qi_img/duoyun.png ",
"Weather": "Cloudy ",
"Wind": "Breeze ",
"Temperature": "22 ~ 16 ℃"
},
            {
"Date": "Wednesday ",
"DayPictureUrl": "yun_qi_img/duoyun.png ",
"Maid": "yun_qi_img/duoyun.png ",
"Weather": "Cloudy ",
"Wind": "Breeze ",
"Temperature": "24 ~ 16 ℃"
},
            {
"Date": "Thursday ",
"DayPictureUrl": "yun_qi_img/duoyun.png ",
"Maid": "yun_qi_img/zhenyu.png ",
"Weather": "multi-cloud to shower ",
"Wind": "Breeze ",
"Temperature": "19 ~ 12 ℃"
            }
        ]
    }
]

The code is as follows:

The weather interface is generally not changed, so it is easy to parse it ..

 
Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. UnsupportedEncodingException;
Import java.net. MalformedURLException;
Import java.net. URL;
Import java.net. URLConnection;
Import java.net. URLEncoder;
 
Import org. apache. tools. ant. types. CommandlineJava. SysProperties;
 
Import net. sf. json. JSONArray;
Import net. sf. json. JSONObject;
 
Public class BaiduWeatherApi {
 
Public static String getWeatherInform (String cityName ){
 
 
String baiduUrl = "http://api.map.baidu.com/telematics/v3/weather? Location = Xiamen & output = json & ak = W69oaDTCfuGwzNwmtVvgWfGH ";
StringBuffer strBuf;
 
Try {
 
BaiduUrl = "http://api.map.baidu.com/telematics/v3/weather? Location ="
+ URLEncoder. encode (cityName, "UTF-8 ")
+ "& Output = json & ak = 57ab4d7f7882e2028de5a9a589ae697f ";
} Catch (UnsupportedEncodingException e1 ){
E1.printStackTrace ();
  }
 
StrBuf = new StringBuffer ();
 
Try {
URL url = new URL (baiduUrl );
URLConnection conn = url. openConnection ();
BufferedReader reader = new BufferedReader (new InputStreamReader (
Conn. getInputStream (), "UTF-8"); // returns a response
String line = null;
While (line = reader. readLine ())! = Null)
StrBuf. append (line + "");
Reader. close ();
} Catch (MalformedURLException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
  }
 
Return strBuf. toString ();
 }
 
 
Public static void main (String [] args ){
 
String strPar = BaiduWeatherApi. getWeatherInform ("Xiamen ");
JSONObject dataOfJson = JSONObject. fromObject (strPar );
System. out. println (dataOfJson );
If (dataOfJson. getInt ("error ")! = 0 ){
System. out. println ("no data retrieved ");
} Else {
   
System. out. println ("date:" + dataOfJson. getString ("date "));
//;
System. out. println (dataOfJson. getString ("results "));
JSONArray array = JSONArray. fromObject (dataOfJson. getString ("results "));
JSONObject data = JSONObject. fromObject (array. get (0). toString ());
System. out. println ("City:" + data. getString ("currentCity "));
JSONArray arrayweather = JSONArray. fromObject (data. getString ("weather_data "));
   
JSONObject weather = JSONObject. fromObject (arrayweather. get (0). toString ());
// "Weather": "Clear ",
// "Wind": "Northeast wind 3-4 ",
// "Temperature": "15 ~ 9 ℃"
System. out. println ("weather:" + weather. getString ("weather "));
System. out. println ("wind direction:" + weather. getString ("wind "));
System. out. println ("temperature:" + weather. getString ("temperature "));
 
   }
  
 }
 
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.