简体   繁体   中英

Unable to parse JSON file in Java

I have a json file in the below format:

{
  "IsLastPage": false,
  "EnrichedData": {
    "Base": [
      {
        "Number": "001107635",
        "Type": "EEG_PR"
        },
        {
        "Number": "00110754",
        "Type": "ABC_PR"
        }
        ]
        }
        }

To parse it in java I am using the following piece of code:

JSONObject jsonObject = new JSONObject(new FileReader("src\\enriched.json"));
JSONArray EnrichedData = jsonObject.getJSONArray("EnrichedData");
System.out.println(EnrichedData);
for(int i=0; i < EnrichedData.length(); i++) {
                JSONObject row = EnrichedData.getJSONObject(i);
                JSONArray elements = row.getJSONArray("Base");

                for(int j=0; j < elements.length(); j++) { 
                    JSONObject element =  elements.getJSONObject(j); 
                    JSONObject Number = element.getJSONObject("Number"); 
                    JSONObject Type = element.getJSONObject("Type"); 

                    System.out.println("Number: " + Number.getString("Number")); 
                    System.out.println("Type: " + Type.getString("Type")); 
                }
            }

But on executing this it throws exception that

org.json.JSONException: JSONObject["Base"] not found.

What wrong am I doing here?

Note that this is wrong:

JSONArray EnrichedData = jsonObject.getJSONArray("EnrichedData");

EnrichedData is not an array but a JSONObject .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM