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

时间:2017-09-18 07:09:07

标签: java

//外部jsonArray文件

{

    "items": [
        {
            "index": 10,
            "index_start_at": 56,
            "integer": 12,
            "float": 16.8248,
            "Firstname": "Natalie",
            "surname": "MacDonald",
            "fullname": "Hilda Rich",
            "email": "eva@durham.jp",
            "Zip": 30988
        },
        {
            "index": 2,
            "index_start_at": 57,
            "integer": 5,
            "float": 13.8932,
            "Firstname": "Jeff",
            "surname": "Miles",
            "fullname": "Meredith Wall",
            "email": "herbert@green.af",
            "Zip": 47888
        },
        {
            "index": 3,
            "index_start_at": 58,
            "integer": 14,
            "float": 10.1125,
            "Firstname": "Mary",
            "surname": "Huff",
            "fullname": "George Schroeder",
            "email": "martha@waller.bo",
            "Zip": 3985
         }
    ]

}

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

     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();
        } 

    }
}

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

,方法键集未定义

1 个答案:

答案 0 :(得分:2)

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

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));
    }
}

<强>更新

以下是所有导入的完整示例:

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));
            }
        }

    }
}