简体   繁体   English

使用Java解析数组中的嵌套JSON对象时出错

[英]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: 尝试解析以下json字符串时, JSONObject["results"] not found错误JSONObject["results"] not found

{
    "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. 您的字符串不是有效的JSON格式,因为您没有在字段之间使用逗号分隔符。 Change your json string to like below and then try. 将您的json字符串更改为如下所示,然后尝试。

{"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 : 您的json字符串格式不正确,缺少一些逗号和引号,可以使用以下格式:

{"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 : 获得json数组结果可以做到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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