[英]How to parse this nested JSON array and JSON object View_ID in android
如何獲取
"View_ID":"v1"
和View
數組。
我創建的內容無法產生正確的響應。 我做錯了什么事?
這是我得到的答復:
{"success":true,"total":2,"View_ID":"v1","View":[{"id":"1","Button_ID":"acc123_b1","Publish_Topic":"\/ziniks\/acc123\/sw1","Subscribe_Topic":"\/ziniks\/acc123\/sw1_status"},{"id":"2","Button_ID":"acc123_b2","Publish_Topic":"\/ziniks\/acc123\/sw2","Subscribe_Topic":"\/ziniks\/acc123\/sw2_status"}]}
我創建以獲得響應的內容:
JSONObject mainObj = new JSONObject(response);
if(mainObj != null){
JSONArray list = mainObj.getJSONArray("View");
if(list != null){
for(int j = 0; j < list.length();j++){
JSONObject innerElem = list.getJSONObject(j);
if(innerElem != null){
String button_id = innerElem.getString("Button_ID");
Toast.makeText(QRCode.this, button_id, Toast.LENGTH_SHORT).show();
}
}
}
}
Try bellow.
JSONObject mainObj = new JSONObject(response); if(mainObj != null){
String viewid = mainObj.getString("View_ID"); // here is your View_ID.
JSONArray list = mainObj.getJSONArray("View"); // here is your view array.
if(list != null){ for(int j = 0; j < list.length();j++){ JSONObject innerElem = list.getJSONObject(j); if(innerElem != null){ String button_id = innerElem.getString("Button_ID"); Toast.makeText(QRCode.this, button_id, Toast.LENGTH_SHORT).show(); } } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.