How to get keys from JSON array?
//外部jsonArray文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | { "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 } ] } |
如何从jsonArray上面获取键并将它们存储在某个数组中,然后在java中随机化这些键的值?
编辑的CODE ......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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 2 3 4 5 6 7 8 9 | 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)); } } |
更新
以下是所有导入的完整示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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)); } } } } |