json结构数据:
Object
{
"code":306000,
"text":"亲,已帮您找到航班信息",
"list":[
{
"flight":"HU496 海南航空 MU2333 东方航空",
"starttime":"20:45 07:40 +1",
"endtime":"",
"icon":"http://unidust.cn/images/api-flight.jpg"
},
Object{...},
Object{...},
Object{...},
Object{...},
Object{...},
Object{...},
Object{...},
Object{...},
Object{...},
Object{...}
]
}
解析过程:
private void parseText(String str) {
String code;
String text;
String flight = null;
String starttime = null;
String endtime = null;
String icon = null;
// 具体怎么解析
try {
JSONObject jsonObject = new JSONObject(str);
//解析字段
code = jsonObject.getString("code");
text = jsonObject.getString("text");
//解析数组
JSONArray jsonArray = jsonObject.getJSONArray("list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
flight = object.getString("flight");
starttime = object.getString("starttime");
endtime = object.getString("endtime");
icon = object.getString("icon");
}
Log.i("wtb", code);
Log.i("wtb", text);
Log.i("wtb", flight);
Log.i("wtb", starttime);
Log.i("wtb", endtime);
Log.i("wtb", icon);
} catch (Exception e) {
e.printStackTrace();
}
}