导航

JSONObject

Posted on   _eve  阅读(224)  评论(0编辑  收藏  举报
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package test;
import java.util.ArrayList;
 
import java.util.List;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class TestJson {
    public static void main(String a[]) {
        String str = "{'weatherReals':[{'cityName':'沈阳','skyStatus':'/','windDirection':'东南风','windSpeed':'2级'," +
                "'maxTemperature':'28.2','minTemperature':'27.8','curTemperature':'27.9','temperature':'/'," +
                "'forcecastRange':'发布时间:2014-05-22 16:00','humidity':'32','precipitation':'0.0','visibility':'/'," +
                "'liveType':'1'},{'cityName':'铁岭','skyStatus':'/','windDirection':'东南风','windSpeed':'2级'," +
                "'maxTemperature':'27.4','minTemperature':'26.6','curTemperature':'26.9','temperature':'/'," +
                "'forcecastRange':'发布时间:2014-05-22 16:00','humidity':'30','precipitation':'0.0'," +
                "'visibility':'/','liveType':'1'}],'cmd':'getWeatherRealList','result':'OK','reason':''}";
        JSONObject jsonobject = JSONObject.fromObject(str);
        try {
            // json字符串转译成json数组对象
            JSONArray array = jsonobject.getJSONArray("weatherReals");
            // 参照json内容建立的对象集合 List<PassPortForLendsEntity>
            List<WeatherLive> list = new ArrayList<WeatherLive>();
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = (JSONObject) array.get(i);
                WeatherLive passport = (WeatherLive) JSONObject.toBean(object, WeatherLive.class);
                System.out.println(passport.getCityName() + "--" + passport.getWindDirection() + "   " + passport.getWindSpeed());
                if (passport != null) {
                    list.add(passport);
                }
            }
        } catch (Exception e) {
 
        }
         
    }
 
}

  

努力加载评论中...
点击右上角即可分享
微信分享提示