How to get
"View_ID":"v1"
andView
Array.
What I create is not generating the proper response. What thing I am doing wrong?
这是我得到的回复:
{"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(); } } } }
关于java - 如何在android中解析这个嵌套的JSON数组和JSON对象View_ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45685261/