Home > Net >  Get key values of nested json objects
Get key values of nested json objects

Time:02-16

I want to get the values of the keys of the next json file using org.json library:

{
    "1": {
        "subject1": "MIS",
        "subject2": "DBMS",
        "subject3": "UML"
    },
    "2": {
        "subject1": "ART",
        "subject2": "MATH",
        "subject3": "MUSIC"
    },
    "3": {
        "subject1": "HISTORY",
        "subject2": "CHEMISTY",
        "subject3": "BIOLOGY"
    }
}

This is what I have tried:

package com.company;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.FileReader;

public class Main {
    public static void main(String[] args) {
        String json = "{\"1\": {\"subject1\": \"MIS\",\"subject2\": \"DBMS\",\"subject3\": \"UML\"},\"2\": {\"subject1\": \"ART\",\"subject2\": \"MATH\",\"subject3\": \"MUSIC\"},\"3\": {\"subject1\": \"HISTORY\",\"subject2\": \"CHEMISTY\",\"subject3\": \"BIOLOGY\"}}";

        try{
            JSONObject root = new JSONObject(json);
            for (int i = 0; i < root.length(); i  ) {
                JSONArray subjects = root.getJSONArray(String.valueOf(i 1));

                for (int j = 0; j < subjects.length(); j  ) {
                    JSONObject number = subjects.getJSONObject(j);
                    String s1 = number.getString("subject1");
                    String s2 = number.getString("subject2");
                    String s3 = number.getString("subject3");
                    System.out.println(s1 ", "  s2 ", " s3);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

With JSONArray subjects = root.getJSONArray(String.valueOf(i 1)); I thought I was getting:

JSONArray subjects = root.getJSONArray("1");
JSONArray subjects = root.getJSONArray("2");
...

But that doesn´t work for me. I just get in console the next:

org.json.JSONException: JSONObject["1"] is not a JSONArray.
    at org.json.JSONObject.wrongValueFormatException(JSONObject.java:2694)
    at org.json.JSONObject.getJSONArray(JSONObject.java:777)
    at com.company.Main.main(Main.java:18)

And I would like to get:

MIS, DBMS, UML
ART, MATH, MUSIC
HISTORI, CHEMISTRY, BIOLOGY

Can you tell me what I am doing wrong? Should I use other library? Thanks for your help.

CodePudding user response:

Subjects is not an array. Arrays in JSON would be enclosed in [ and ].To get the subjects, you would need follow below approach.

try{
     JSONObject root = new JSONObject(json);
     for (int i = 0; i < 3; i  ) {
        JSONObject Obj = root.getJSONObject(String.valueOf(i 1));
        String s1 = Obj.getString("subject1");
        String s2 = Obj.getString("subject2");
        String s3 = Obj.getString("subject3");
        System.out.println(s1 ", "  s2 ", " s3);
      }
  }catch(Exception e){
            e.printStackTrace();
  }

CodePudding user response:

You got it all correct until this point

JSONArray subjects = root.getJSONArray(String.valueOf(i 1));

you have to get JSONObject not JSONArray since you don't have an array. Your second mistake is this loop

for (int j = 0; j < subjects.length(); j  ) {}

since you're already getting the object manually you don't have to loop.

Final Code

    String json = "{\"1\": {\"subject1\": \"MIS\",\"subject2\": \"DBMS\",\"subject3\": \"UML\"},\"2\": {\"subject1\": \"ART\",\"subject2\": \"MATH\",\"subject3\": \"MUSIC\"},\"3\": {\"subject1\": \"HISTORY\",\"subject2\": \"CHEMISTY\",\"subject3\": \"BIOLOGY\"}}";

    try {
        JSONObject root = new JSONObject(json);
        for (int i = 0; i < root.length(); i  ) {
            JSONObject number = root.getJSONObject(String.valueOf(i   1));
            String s1 = number.getString("subject1");
            String s2 = number.getString("subject2");
            String s3 = number.getString("subject3");
            System.out.println(s1   ", "   s2   ", "   s3);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

OUTPUT

MIS, DBMS, UML
ART, MATH, MUSIC
HISTORY, CHEMISTY, BIOLOGY
  • Related