java - 如何在android中解析这个嵌套的JSON数组和JSON对象View_ID

标签 java android json

How to get "View_ID":"v1" and View 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/

相关文章:

java - 唤醒java中等待的线程

java - 如何使用句柄在 android 中缩放 View ?

javascript - Knockout.js 错误 : Unable to parse bindings. 消息:ReferenceError:myData 未定义;绑定(bind)值:with:myData

java - 最好使用正则表达式或 Stringtokenizer 在此 : William Faulkner - 'Light In August' 中查找作者和书名

java - 蔚来。监听同一个端口的多个客户端 channel

java - 如何使用 java 访问 android 中的嵌套 JSON 数据?

ios - 在 iOS 中编辑 URL 字符串

c# - 如何计算 HttpRequestMessage 的大小?

java - 在 JPanel 与 JComponent 中绘图

Android Canvas 屏幕分辨率问题