解析中国天气网的json代码:
- public class MyWeather {
- public static String city;
- public static String temp1;
- public static String weather1;
- public static String img1;
- public static void getWeather() {
- try {
- URL url = new URL("http://m.weather.com.cn/data/101250101.html");
- InputStream is = url.openStream();
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- int len = -1;
- byte[] buffer = new byte[1024];
- while ((len = is.read(buffer)) != -1) {
- bos.write(buffer, 0, len);
- }
- String info = bos.toString("utf-8");
- JSONObject dataJson = new JSONObject(info);
- JSONObject json = dataJson.getJSONObject("weatherinfo");
- city = json.getString("city");
- temp1 = json.getString("temp1");
- weather1 = json.getString("weather1");
- img1 = json.getString("img1");
- System.out.println(city);is.close();bos.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }