解析JSon數據

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();
	}
}


發佈了31 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章