gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:22:11 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com