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

时间:2018-12-28 08:23:13

标签: java json eclipse parsing

尝试解析以下json字符串时出现错误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);

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您的字符串不是有效的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}}}

答案 1 :(得分:0)

您的json字符串格式不正确,缺少一些逗号和引号, 可以使用这个:

// here data set gets filtered based on users selection and is sent back
// Using ArrayLib library
function functionToRunOnFormSubmit(y,z)  {
  var ss = SpreadsheetApp.openById(id);
  var ActiveSheet = ss.getSheetByName("TogglMap");
  var StartRow = 2;
  var RowRange = ActiveSheet.getLastRow() - StartRow + 1;

  var EMWholeRange = ActiveSheet.getRange(StartRow,2,RowRange,13);
  var AllValues = EMWholeRange.getDisplayValues();

  var dat = y +'-01'

  var removeCol = function(arr, colIndex, colIndex2) {
    for (var i = 0; i < arr.length; i++) {
      var row = arr[i];
      row.splice(colIndex, colIndex2);
    }
  }

  removeCol(AllValues, 5 , 6);
  var filteredArr1 =  ArrayLib.filterByText(AllValues, 1, z)

  var filteredArr2 =  ArrayLib.filterByText(filteredArr1, 3, dat)
  removeCol(filteredArr2, 1 ,1);

  Logger.log(AllValues)
  return filteredArr2
};

获取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}}}