导航

JSONObject

Posted on 2017-02-17 09:24  _eve  阅读(223)  评论(0编辑  收藏  举报
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) {

		}
		
	}

}