java 如何从 JSON 数组中获取密钥?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46273493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:08:37  来源:igfitidea点击:

How to get keys from JSON array?

java

提问by ASM

//External jsonArray file

//外部jsonArray文件

{

    "items": [
        {
            "index": 10,
            "index_start_at": 56,
            "integer": 12,
            "float": 16.8248,
            "Firstname": "Natalie",
            "surname": "MacDonald",
            "fullname": "Hilda Rich",
            "email": "[email protected]",
            "Zip": 30988
        },
        {
            "index": 2,
            "index_start_at": 57,
            "integer": 5,
            "float": 13.8932,
            "Firstname": "Jeff",
            "surname": "Miles",
            "fullname": "Meredith Wall",
            "email": "[email protected]",
            "Zip": 47888
        },
        {
            "index": 3,
            "index_start_at": 58,
            "integer": 14,
            "float": 10.1125,
            "Firstname": "Mary",
            "surname": "Huff",
            "fullname": "George Schroeder",
            "email": "[email protected]",
            "Zip": 3985
         }
    ]

}

how to get keys from above jsonArray and storing those in some array and then randomize the values of those keys in java?? Edited CODE...

如何从上面的 jsonArray 获取键并将它们存储在某个数组中,然后在 java 中随机化这些键的值?编辑代码...

     import java.io.FileNotFoundException;
        import java.io.FileReader;
        import java.io.IOException;
        import java.util.Iterator;

        import org.json.JSONException;
        import org.json.JSONObject;
        import org.json.simple.JSONArray;
        import org.json.simple.parser.JSONParser;
        import org.json.simple.parser.ParseException;




        public class JSONReadFromFile {


            public static void main(String[] args) throws JSONException {

                JSONParser parser = new JSONParser();


                String jsonString=null;

                Object Obj;

                //JSONObject element;

                try {

                    Obj = parser.parse(new FileReader("jsonArray.json"));
                    System.out.println(Obj);
                    jsonString=Obj.toString();

                    JSONObject object = new JSONObject(jsonString); //jsonString = String from the file
                    org.json.JSONArray array = object.getJSONArray("items");
                    Iterator<Object> iterator = array.iterator();
                    while(iterator.hasNext()){
                        JSONObject jsonObject = (JSONObject) iterator.next();
                        for(String key : jsonObject.keySet()){
                            System.out.println(key + ":" + jsonObject.get(key));
                        }
                    }

        }

}
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ParseException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } 

    }
}

I am trying to do it like this .. is this correct way of doing ?? Firstly i am reading the json file and then extracting the keys from it. here in above code i am getting two errors---- The method iterator is undefined for the type jsonArray && The method keyset is undefined for the type jsonArray

我正在尝试这样做..这是正确的做法吗??首先,我正在读取 json 文件,然后从中提取密钥。在上面的代码中,我遇到两个错误---- jsonArray 类型的方法迭代器未定义 && jsonArray 类型的方法键集未定义

回答by Darshan Mehta

You can do it with two loops, e.g.:

你可以用两个循环来完成,例如:

JSONObject object = new JSONObject(jsonString); //jsonString = String from the file
JSONArray array = object.getJSONArray("items");
Iterator<Object> iterator = array.iterator();
while(iterator.hasNext()){
    JSONObject jsonObject = (JSONObject) iterator.next();
    for(String key : jsonObject.keySet()){
        System.out.println(key + ":" + jsonObject.get(key));
    }
}

Update

更新

Here's the full example with all the imports:

这是包含所有导入的完整示例:

import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;

public class Test {

    public static void main(String[] args) {

        JSONObject object = new JSONObject("{\"items\":[{\"index\":10}]}");
        JSONArray array = object.getJSONArray("items");
        Iterator<Object> iterator = array.iterator();

        while (iterator.hasNext()) {
            JSONObject jsonObject = (JSONObject) iterator.next();

            for (String key : jsonObject.keySet()) {
                System.out.println(key + ":" + jsonObject.get(key));
            }
        }

    }
}