Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中读取json文件中json对象数组的属性_Java_Json_Spring Boot - Fatal编程技术网

在java中读取json文件中json对象数组的属性

在java中读取json文件中json对象数组的属性,java,json,spring-boot,Java,Json,Spring Boot,我不熟悉java中的JSON数组/对象。在这里,我正在努力获取JSON对象的属性。我的尝试如下 JSONParser jsonParser = new JSONParser(); try(FileReader reader = new FileReader("players.json")){ //Read JSON file

我不熟悉java中的JSON数组/对象。在这里,我正在努力获取JSON对象的属性。我的尝试如下

    JSONParser jsonParser = new JSONParser();
    try(FileReader reader = new FileReader("players.json")){                                     
       //Read JSON file                                  
       Object obj = jsonParser.parse(reader);                                                                
       JSONArray playersList = (JSONArray) obj; 
       //Below is the one which is having compilation issues                                                                 
       System.out.println(playersList.get(1).getString("name")); 

    } catch (FileNotFoundException e) {                                  
       // TODO Auto-generated catch block

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

          e.printStackTrace();
     } catch (ParseException e) {                                    
       // TODO Auto-generated catch block                                    
          e.printStackTrace();
    }
      [
       {
         "_id": 1, 
         "name": "greg",
       },
       {
         "_id": 2,   
         "name": "freg gre",
       }
      ]
在这里,我试图获取JSON数组中第二个对象的名称。但我找不到如上所述调用getString(“name”)的方法。所以我非常感谢你的帮助

Json文件如下所示

    JSONParser jsonParser = new JSONParser();
    try(FileReader reader = new FileReader("players.json")){                                     
       //Read JSON file                                  
       Object obj = jsonParser.parse(reader);                                                                
       JSONArray playersList = (JSONArray) obj; 
       //Below is the one which is having compilation issues                                                                 
       System.out.println(playersList.get(1).getString("name")); 

    } catch (FileNotFoundException e) {                                  
       // TODO Auto-generated catch block

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

          e.printStackTrace();
     } catch (ParseException e) {                                    
       // TODO Auto-generated catch block                                    
          e.printStackTrace();
    }
      [
       {
         "_id": 1, 
         "name": "greg",
       },
       {
         "_id": 2,   
         "name": "freg gre",
       }
      ]

你可以像下面这样使用

  JSONObject jsonObject = (JSONObject)playersList.get(1);
  String name = (String) jsonObject.get("name");
JSONParser-JSONParser=new-JSONParser();
try(FileReader=newfilereader(“players.json”){
objectobj=jsonParser.parse(reader);
JSONArray JSONArray=新JSONArray(obj);
for(int i=0;i

您只需要遍历数组中的每个对象,每个对象都有键和值,可以在循环中获取它。

您可以展示一些示例player.json吗?我已经用json文件更新了这个问题,现在编译问题是什么?(您添加了依赖项吗?(关于json simple))getJSONObject方法中也存在编译问题。您使用的是哪个JSON库?org.JSON.simple.*更新了答案,答案很有用。但是getString()方法也有问题。
String name=(String)jsonObject.get(“name”);