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) { } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步