How to get Json in Android

Json包如下

[{"cityinfo":{"areaid":"101010100","namecn":"北京","districtcn":"北京","provcn":"北京","nationcn":"中国","lon":"116.391","lat":"39.904","time":"20140120144758","time_zone":"GMT+8:00","zone_abb":"CST","dst":"0"}},{"l":{"l1":"3","l2":"19","l3":"4","l4":"8","l5":"00","l6":"0","l7":"14:35"}}]。

包中共有2个分组,故要取得分别对应的数值,可做如下处理

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
	builder.append(s);
}
Log.i("json_str", builder.toString());
jsonArray = new JSONArray(builder.toString());
JSONObject jsonObject = jsonArray.getJSONObject(0);
Log.i("city", jsonObject.getJSONObject("cityinfo").getString("namecn"));
jsonObject = jsonArray.getJSONObject(1);
Log.i("temp", jsonObject.getJSONObject("l").getString("l1"));
Log.i("wather", jsonObject.getJSONObject("l").getString("l5"));


如此可得到cityinfo分组和l分组中的各个键值对。

参考http://stackoverflow.com/questions/14929734/android-how-to-get-pageid-and-title-ns-keys-from-valid-json-data

你可能感兴趣的:(How to get Json in Android)