简体   繁体   中英

Error on parsing nested JSON objects in an array using Java

I am getting an error JSONObject["results"] not found when trying to parse the below json string:

{
    "serviceModel": {
        "results": [
            {
                "application": {
                    "applicationId": 1234"applicationStatus": "Determined""applicationSubmittedDate": "2013-09-19 12:00:00 AM""applicationType": "Initial""asOfDate": "2018-12-28 02:42:26 AM""logicalApplicationId": "1234"
                }"contact": {
                    "addresses": [
                        {
                            "city": "Bridgeport""county": "Fairfield""line1": "123 Main Road""state": "CT""type": "H""zipCode": "12345"
                        }{
                            "city": "Bridgeport""county": "Fairfield""line1": "123 Main Road""state": "CT""type": "M""zipCode": "12345"
                        }
                    ]"dob": "1900-01-01""firstName": "John""gender": "MALE""homelessIndicator": "N""lastName": "Smith""otherIds": []"personId": "11233""preferredLanguage": "E""primaryApplicantIndicator": "Y""ssn": "123456789"
                }
            }
        ]"summary": {
            "resultCount": 1
        }
    }
}

And below is my current code (although I have tried many different iterations):

JSONObject jsonResults = new JSONObject(sb.toString());
JSONArray array = jsonResults.getJSONArray("results");
for (int index=0;index<array.length();index++)
    {
JSONObject obj=array.getJSONObject(index);
if (!obj.isNull("ssn"))
    }
ssn = obj.getString("ssn");
data.addToLog("SSN"+ String.valueOf(index),ssn);
    }
       }
data.addToLog("SSN",ssn);

Any help is appreciated!

Your string is not in valid JSON format, as you are not using comma separators between fields. Change your json string to like below and then try.

{"serviceModel":{"results":[{"application":{"applicationId":1234,"applicationStatus":"Determined","applicationSubmittedDate":"2013-09-19 12:00:00 AM","applicationType":"Initial","asOfDate":"2018-12-28 02:42:26 AM","logicalApplicationId":"1234"},
"contact":{"addresses":[
  {"city":"Bridgeport","county":"Fairfield","line1":"123 Main Road","state":"CT","type":"H","zipCode":"12345"},
  {"city":"Bridgeport","county":"Fairfield","line1":"123 Main Road","state":"CT","type":"M","zipCode":"12345"}],
  "dob":"1900-01-01","firstName":"John","gender":"MALE","homelessIndicator":"N","lastName":"Smith",
  "otherIds":[],
  "personId":"11233",
  "preferredLanguage":"E",
  "primaryApplicantIndicator":"Y",
  "ssn":"123456789"
}}],"summary":{"resultCount":1}}}

your json string is not well formatted, missing some comma and quote, can use this one :

{"serviceModel":{"results":[{"application":{"applicationId":"1234","applicationStatus":"Determined","applicationSubmittedDate":"2013-09-19 12:00:00 AM","applicationType":"Initial","asOfDate":"2018-12-28 02:42:26 AM","logicalApplicationId":"1234"},"contact":{"addresses":[{"city":"Bridgeport","county":"Fairfield","line1":"123 Main Road","state":"CT","type":"H","zipCode":"12345"},{"city":"Bridgeport","county":"Fairfield","line1":"123 Main Road","state":"CT","type":"M","zipCode":"12345"}],"dob":"1900-01-01","firstName":"John","gender":"MALE","homelessIndicator":"N","lastName":"Smith","otherIds":[],"personId":"11233","preferredLanguage":"E","primaryApplicantIndicator":"Y","ssn":"123456789"}}],"summary":{"resultCount":1}}}

to get json array results can do :

JSONArray array = jsonResults.getJsonObject("serviceModel").getJsonArray("results");

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