如果 Json 資料如下:
{
"status": 200,
"message": "取得系統靜態文字資料成功!",
"table_info": [
{
"question_type_code": "01",
"type_name": "詢問使用方式"
},
{
"question_type_code": "02",
"type_name": "問題回報"
},
{
"question_type_code": "03",
"type_name": "建議"
},
{
"question_type_code": "99",
"type_name": "其他"
}
]
}
處理 function 可以這麼寫
private void JsonProcess(String responseJson) { JSONObject response; try { response = new JSONObject(responseJson); String status = response.getString("status"); System.out.println(TAG + "status:" + status); if (status.equals("200")) { String message = response.getString("message"); System.out.println(TAG + "message:" + message); JSONArray tableInfoList = response.getJSONArray("table_info"); for (int i = 0; i < tableInfoList.length(); i++) { String tableInfoJson = tableInfoList.getString(i); System.out.println("tableInfoJson:" + tableInfoJson); JSONObject tableInfoContent = new JSONObject(tableInfoJson); String questionTypeCode = tableInfoContent.getString("question_type_code"); String typeName = tableInfoContent.getString("type_name"); System.out.println(TAG + "questionTypeCode i [" + i + "]:" + questionTypeCode); System.out.println(TAG + "typeName i [" + i + "]:" + typeName); } } else { String error = response.getString("error"); } } catch (JSONException e) { e.printStackTrace(); } }
沒有留言:
張貼留言