在android中获取json数组键

 mobiledu2502902687 发布于 2023-02-13 14:59

K先生给出的答案也是对的,但您也可以使用jsonObject names()方法.请找到示例代码

for(int i = 0; i
        
        
    
3 个回答
  • 上面的示例json数组,如何获得204,203和202?

    不,当前的String JSONObject代替JSONArray.你应该使用JSONObject获取Iterator .keys()如果内部JSONObject键动态为:

    JSONObject issueObj = new JSONObject(jsonContent);
    Iterator iterator = issueObj.keys();
       while(iterator.hasNext()){
        String key = (String)iterator.next();
        JSONObject issue = issueObj.getJSONObject(key);
    
        //  get id from  issue
            String _pubKey = issue.optString("id");
        }
    

    2023-02-13 15:01 回答
  • K先生给出的答案也是对的,但您也可以使用jsonObject names()方法.请找到示例代码

    for(int i = 0; i<jsonobject.length(); i++){
        Log.e(TAG, "Key = " + jsonobject.names().getString(i) + " value = " + jsonobject.get(jsonobject.names().getString(i)));
    }
    

    我希望dis会帮助你

    2023-02-13 15:02 回答
  • 使用此方法动态迭代json

    private void parseJson(JSONObject data) {
    
            if (data != null) {
                Iterator<String> it = data.keys();
                while (it.hasNext()) {
                    String key = it.next();
    
                    try {
                        if (data.get(key) instanceof JSONArray) {
                            JSONArray arry = data.getJSONArray(key);
                            int size = arry.length();
                            for (int i = 0; i < size; i++) {
                                parseJson(arry.getJSONObject(i));
                            }
                        } else if (data.get(key) instanceof JSONObject) {
                            parseJson(data.getJSONObject(key));
                        } else {
                            System.out.println("" + key + " : " + data.optString(key));
                        }
                    } catch (Throwable e) {
                        System.out.println("" + key + " : " + data.optString(key));
                        e.printStackTrace();
    
                    }
                }
            }
        }
    

    2023-02-13 15:02 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有