0

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",
       }
      ]
3
  • Can you show some sample player.json ? Commented Oct 18, 2019 at 18:10
  • I have updated the question with the json file now Commented Oct 18, 2019 at 18:26
  • what is the compilation issue? (have you added the dependency? (on json-simple)) Commented Oct 18, 2019 at 19:29

2 Answers 2

1

You can use like following

  JSONObject jsonObject = (JSONObject)playersList.get(1);
  String name = (String) jsonObject.get("name");
Sign up to request clarification or add additional context in comments.

6 Comments

There also compilation issue in getJSONObject method.
which JSON library are you using?
org.json.simple.*
Updated the answer
That answer was useful. But getString() method also having issues. String name = (String) jsonObject.get("name"); This worked
|
0
JSONParser jsonParser = new JSONParser();
    try(FileReader reader = new FileReader("players.json")){                                     

       Object obj = jsonParser.parse(reader);    
 JSONArray jsonArray = new JSONArray(obj);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject json = jsonArray.getJSONObject(i);
        Iterator<String> keys = json.keys();

        while (keys.hasNext()) {
            String key = keys.next();
            System.out.println("Key :" + key + "  Value :" + json.get(key));
        }

    }

you just need to iterate through each object inside array and each object has keys and values fetch it within a loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.