I am new to JSON array/objects in the java. Here I am struggling to get the property of a JSON object. My attempt is as follows.
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();
}
There I am trying to get the name of the second object in the JSON array. But I couldn't find a way to call getString("name") as above. So I highly appreciate your help for this.
Json file is as follows.
[
{
"_id": 1,
"name": "greg",
},
{
"_id": 2,
"name": "freg gre",
}
]