簡體   English   中英

使用Java解析數組中的嵌套JSON對象時出錯

[英]Error on parsing nested JSON objects in an array using Java

嘗試解析以下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
        }
    }
}

下面是我當前的代碼(盡管我嘗試了許多不同的迭代):

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);

任何幫助表示贊賞!

您的字符串不是有效的JSON格式,因為您沒有在字段之間使用逗號分隔符。 將您的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}}}

您的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}}}

獲得json數組結果可以做到:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM