Java 如何在android中解析这个嵌套的JSON数组和JSON对象视图ID
如何获取Java 如何在android中解析这个嵌套的JSON数组和JSON对象视图ID,java,android,json,Java,Android,Json,如何获取“视图ID”:“v1”和视图数组。 我创建的内容没有产生正确的响应。我做错了什么? 以下是我得到的答复: {"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
“视图ID”:“v1”
和视图
数组。我创建的内容没有产生正确的响应。我做错了什么? 以下是我得到的答复:
{"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();
}
}
}
}
JSONObject mainObj=新的JSONObject(响应);
如果(mainObj!=null){
JSONArray list=mainObj.getJSONArray(“视图”);
如果(列表!=null){
对于(int j=0;j
试试下面的方法。
JSONObject mainObj=新的JSONObject(响应);如果(mainObj!=null){
String viewid=mainObj.getString(“视图ID”);//这是您的视图ID。
JSONArray list=mainObj.getJSONArray(“视图”);//这是您的视图数组。
if(list!=null){for(int j=0;j
您可以在其中添加日志
。我尝试使用code。这没关系。如果在代码中添加if(response!=null){}
。
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(); } } } }