看了Gson、FastJson解析,确实简便了很多,但是如果key关键字有重复的怎么办呢,
而且我要解析的Json比较复杂,综合后还是考虑用JsonObject、JsonArray比较好
下面我按部分来说明
1 这是最简单的一种
"pm25":{
"key":"Hefei",
"show_desc":0,
"pm25":{
"curPm":"112",
"pm25":"84",
"pm10":"114",
"level":3,
"quality":"轻度污染",
"des":"轻微污染 易感人群症状有轻度加剧,健康人群出现刺激症状 心脏病和呼吸系统疾病患者应减少体力消耗和户外活动。"
},
"dateTime":"2016年10月06日11时",
"cityName":"合肥"
},
如下:
///解析当前空气
JSONObject data = new JSONObject(resultString);//resultString是需要解析的字符串
JSONObject pM25JsonObject = data.getJSONObject("pm25");
JSONObject curPm25JsonObject =pM25JsonObject.getJSONObject("pm25");
Log.i("当前PM25:",curPm25JsonObject.getString("curPm"));
Log.i("当前空气等级:",curPm25JsonObject.getString("level"));
Log.i("当前空气质量:",curPm25JsonObject.getString("quality"));
Log.i("当前空气描述:",curPm25JsonObject.getString("des"));
2 对象里是数组
"info":{
"chuanyi":[
"舒适",
"建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"
],
"ganmao":[
"较易发",
"虽然温度适宜但风力较大,仍较易发生感冒,体质较弱的朋友请注意适当防护。"
],
"kongtiao":[
"较少开启",
"您将感到很舒适,一般不需要开启空调。"
],
"xiche":[
"不宜",
"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"
],
"yundong":[
"较适宜",
"阴天,较适宜进行各种户内外运动。"
],
"ziwaixian":[
"最弱",
"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"
]
}
如下: JSONObject info = new JSONObject(string);
Iterator it = info.keys();///chuanyi ganmao
while(it.hasNext()){
String key = (String)it.next();
JSONArray indexArray = info.getJSONArray(key);
if(key.equals("chuanyi")){
Log.i("穿衣是:", "指数:"+indexArray.getString(0)+" 描述:"+indexArray.getString(1));
}
if(key.equals("ganmao")){
Log.i("感冒是:", "指数:"+indexArray.getString(0)+" 描述:"+indexArray.getString(1));
}
if(key.equals("kongtiao")){
Log.i("空调是:", "指数:"+indexArray.getString(0)+" 描述:"+indexArray.getString(1));
}
if(key.equals("xiche")){
Log.i("洗车是:", "指数:"+indexArray.getString(0)+" 描述:"+indexArray.getString(1));
}
if(key.equals("yundong")){
Log.i("运动是:", "指数:"+indexArray.getString(0)+" 描述:"+indexArray.getString(1));
}
if(key.equals("ziwaixian")){
Log.i("紫外线是:", "指数:"+indexArray.getString(0)+" 描述:"+indexArray.getString(1));
}
}
3 对象数组对象
"f3h":{
"temperature":[
{
"jg":"20161006110000",
"jb":"23"
},
{
"jg":"20161006140000",
"jb":"25"
},
{
"jg":"20161006170000",
"jb":"25"
},
{
"jg":"20161006200000",
"jb":"22"
},
{
"jg":"20161006230000",
"jb":"21"
},
{
"jg":"20161007020000",
"jb":"20"
},
{
"jg":"20161007050000",
"jb":"20"
},
{
"jg":"20161007080000",
"jb":"20"
},
{
"jg":"20161007110000",
"jb":"22"
}
]
}
如下:
JSONObject f3h = new JSONObject(string);
JSONArray f3hTemperatureArray = f3h.getJSONArray("temperature");
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(0).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(0).getString("jb"));
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(1).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(1).getString("jb"));
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(2).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(2).getString("jb"));
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(3).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(3).getString("jb"));
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(4).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(4).getString("jb"));
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(5).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(5).getString("jb"));
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(6).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(6).getString("jb"));
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(7).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(7).getString("jb"));
Log.i("24h的预测温度:", "时间:"+f3hTemperatureArray.getJSONObject(8).getString("jg")+" 温度:"+f3hTemperatureArray.getJSONObject(8).getString("jb"));
4 数组对象对象数组
"weather":[
{
"date":"2016-10-07",
"info":{
"dawn":[
"7",
"小雨",
"19",
"东北风",
"4-5 级",
"17:49"
],
"day":[
"7",
"小雨",
"22",
"东北风",
"3-4 级",
"06:08"
],
"night":[
"1",
"多云",
"17",
"西南风",
"微风",
"17:48"
]
},
"week":"五",
"nongli":"九月初七"
},
{
"date":"2016-10-08",
"info":{
"dawn":[
"1",
"多云",
"17",
"西南风",
"微风",
"17:48"
],
"day":[
"1",
"多云",
"24",
"东北风",
"微风",
"06:09"
],
"night":[
"1",
"多云",
"16",
"东北风",
"3-4 级",
"17:47"
]
},
"week":"六",
"nongli":"九月初八"
}
]
JSONArray weather_array = new JSONArray(string);
// ////////////////////////////////////////////////////////////////////////当天
Log.i("今天的阳历是:", weather_array.getJSONObject(0).getString("date"));
Log.i("今天的星期是:", weather_array.getJSONObject(0).getString("week"));
Log.i("今天的农历是:", weather_array.getJSONObject(0).getString("nongli"));
JSONObject today_info = weather_array.getJSONObject(0).getJSONObject("info");
Iterator today_it = today_info.keys();
while (today_it.hasNext()) {
String today_key = (String) today_it.next();
JSONArray today_weather_info_array = today_info.getJSONArray(today_key);
/* "day": [
"7",
"小雨",
"25",
"东北风",
"4-5 级",
"06:08"
], */
if (today_key.equals("dawn")) {
Log.i("今天凌晨的温度是:", today_weather_info_array.getString(2));
}
if (today_key.equals("day")) {
Log.i("今天白天的天气标识是:", today_weather_info_array.getString(0));
Log.i("今天白天的天气是:", today_weather_info_array.getString(1));
Log.i("今天白天的温度是:", today_weather_info_array.getString(2));
Log.i("今天白天的风向是:", today_weather_info_array.getString(3));
Log.i("今天白天的风力是:", today_weather_info_array.getString(4));
Log.i("今天白天的日出时间是:", today_weather_info_array.getString(5));
}
if (today_key.equals("night")) {
Log.i("今天夜间的天气标识是:", today_weather_info_array.getString(0));
Log.i("今天夜间的天气是:", today_weather_info_array.getString(1));
Log.i("今天夜间的温度是:", today_weather_info_array.getString(2));
Log.i("今天夜间的风向是:", today_weather_info_array.getString(3));
Log.i("今天夜间的风力是:", today_weather_info_array.getString(4));
Log.i("今天夜间的日落时间是:", today_weather_info_array.getString(5));
}
}///while,遍历三个时间段的循环
// ////////////////////////////////////////////////////////////////////////第2天
Log.i("第2天的阳历是:", weather_array.getJSONObject(1).getString("date"));
Log.i("第2天的星期是:", weather_array.getJSONObject(1).getString("week"));
Log.i("第2天的农历是:", weather_array.getJSONObject(1).getString("nongli"));
JSONObject day2_info = weather_array.getJSONObject(1).getJSONObject("info");
Iterator day2_it = day2_info.keys();
while (day2_it.hasNext()) {
String day2_key = (String) day2_it.next();
JSONArray day2_weather_info_array = day2_info.getJSONArray(day2_key);
/* "day": [
"7",
"小雨",
"25",
"东北风",
"4-5 级",
"06:08"
], */
if (day2_key.equals("dawn")) {
Log.i("第2天凌晨的温度是:", day2_weather_info_array.getString(2));
}
if (day2_key.equals("day")) {
Log.i("第2天白天的天气标识是:", day2_weather_info_array.getString(0));
Log.i("第2天白天的天气是:", day2_weather_info_array.getString(1));
Log.i("第2天白天的温度是:", day2_weather_info_array.getString(2));
Log.i("第2天白天的风向是:", day2_weather_info_array.getString(3));
Log.i("第2天白天的风力是:", day2_weather_info_array.getString(4));
//Log.i("第2天白天的日出是:", day2_weather_info_array.getString(5));
}
if (day2_key.equals("night")) {
Log.i("第2天夜间的天气标识是:", day2_weather_info_array.getString(0));
Log.i("第2天夜间的天气是:", day2_weather_info_array.getString(1));
Log.i("第2天夜间的温度是:", day2_weather_info_array.getString(2));
Log.i("第2天夜间的风向是:", day2_weather_info_array.getString(3));
Log.i("第2天夜间的风力是:", day2_weather_info_array.getString(4));
//Log.i("第2天夜间的日落是:", day2_weather_info_array.getString(5));
}
}///while,遍历三个时间段的循环
亲测可用
总结如下:
{ }是对象,需要是JsonObject.getString(str)
[ ]是数组,需要是JsonArray.getString(i)
{[ ] ,[ ]}对象里是数组,需要用JsonObject Iterator while循环
[{ },{ }]数组里是对象,需要用JsonArray.getJsonObject(i)