代码之家  ›  专栏  ›  技术社区  ›  Ladu anand

从服务调用springjava解析JSON对象

  •  0
  • Ladu anand  · 技术社区  · 7 年前

    org.json.JSONException: JSONArray initial value should be a string or collection or array.
            at org.json.JSONArray.<init>(JSONArray.java:197) ~[json-20180813.jar!/:na]
    

                Object resp = hiveApiClient.getEnrollmentSearchDetails(certificate, employeeId);
                logger.info("response : " + resp);
                 JSONArray mainArray = new JSONArray(resp);
    
                 // The nested array is at the second position : 1
                 JSONArray nestedArray = mainArray.getJSONArray(1);
    
                 // the interesting main JSONObject is on the first position 
                 // of the nested array : 0
                 JSONObject interestingJSONObject = nestedArray.getJSONObject(0);
                 logger.info("XXX :{}", interestingJSONObject);
                 String courseId = interestingJSONObject.getJSONObject("additionalData").getString("courseId");
                 logger.info("XXXX :{}",courseId);
                return courseId;
    

    答复:

    [
        "list", [{
            "@type": "com.saba.services.calendar.CalendarElementDetail",
            "eventType": "ILTCLASS",
            "elementName": "Microservice Application Architecture",
            "elementId": "class000000000013497",
            "eventId": "timel000000000103609",
            "ownerID": "emplo000000000096641",
            "locationId": "locat000000000003165",
            "locationName": "IND-Bangalore-Karnataka",
            "additionalData": {
                "@type": "map",
                "locationTimeZone": "tzone000000000000042",
                "eventID": "class000000000013497",
                "locationName": "IND-Bangalore-Karnataka",
                "locationId": "locat000000000003165",
                "transcriptID": "ofapr000000002962367",
                "registrationID": "regdw000000001766254",
                "eventName": "Microservice Application Architecture",
                "moduleID": "regmd000000002147176",
                "courseID": "cours000000000031995"
            },
            "startDate": {
                "@type": "com.saba.customtypes.DateWithLocale",
                "date": 1538613000000,
                "locale": "03-OCT-2018",
                "timeInLocale": "8:30 PM",
                "dateInUserTimeZone": "03-OCT-2018",
                "timeInUserTimeZone": "5:30 PM",
                "dateInCustomTimeZone": null,
                "timeInCustomTimeZone": null,
                "customTimeZoneDate": 0,
                "timeInStandardFormat": "8:30 PM",
                "dateInStandardFormat": "10/03/2018"
            }
        }]
    ]
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Community Mohan Dere    5 年前

    首先,你的json是无效的,因为}:

    ["list" : /* something here but anyway, not the concern here */ ]
    

    当它应该是

    {"list" : /* something here but anyway not the concern here */}
    

    我认为你的问题在于理解JSON文件是如何工作的,什么是JSON对象和JSON数组。请更正您的JSON输入,以便我们能够为您提供有关如何检索所需值的见解。

    另外,我建议你 Jackson 用于将JSON对象直接解析为javapojo的lib非常容易。链接是一个伟大的教程,让你开始在这里。此外,jackson已经包含在Spring中了,所以您实际上没有什么要安装的。

    我误读了JSON输入:我看到一个 : 之后 "list" 而不是 , .

    那么你怎么才能真正得到你的价值呢?让我们来描述一下JSON,这里有一个JSON数组,包含一个字符串和另一个子JSON数组。您需要从嵌套JSON数组中的第一个JSON对象中获取一些值。

    这个:

     {
        "@type": "com.saba.services.calendar.CalendarElementDetail",
        "eventType": "ILTCLASS",
        "elementName": "Microservice Application Architecture",
        "elementId": "class000000000013497",
        "eventId": "timel000000000103609",
        "ownerID": "emplo000000000096641",
        "locationId": "locat000000000003165",
        "locationName": "IND-Bangalore-Karnataka",
        "additionalData": {
            "@type": "map",
            "locationTimeZone": "tzone000000000000042",
            "eventID": "class000000000013497",
            "locationName": "IND-Bangalore-Karnataka",
            "locationId": "locat000000000003165",
            "transcriptID": "ofapr000000002962367",
            "registrationID": "regdw000000001766254",
            "eventName": "Microservice Application Architecture",
            "moduleID": "regmd000000002147176",
            "courseID": "cours000000000031995"
        },
        "startDate": {
            "@type": "com.saba.customtypes.DateWithLocale",
            "date": 1538613000000,
            "locale": "03-OCT-2018",
            "timeInLocale": "8:30 PM",
            "dateInUserTimeZone": "03-OCT-2018",
            "timeInUserTimeZone": "5:30 PM",
            "dateInCustomTimeZone": null,
            "timeInCustomTimeZone": null,
            "customTimeZoneDate": 0,
            "timeInStandardFormat": "8:30 PM",
            "dateInStandardFormat": "10/03/2018"
        }
    }
    

    这里的第一个任务是收集这个物体。假设嵌套的json数组总是在字符串后面的第二个位置,并且您想要的json对象总是在嵌套数组的第一个位置,这可能不是根据您的输入json而定的情况,但是这在您的问题中没有被精确化。

     JSONArray mainArray = new JSONArray(resp);
    
     // The nested array is at the second position : 1
     JSONArray nestedArray = mainArray.getJSONArray(1);
    
     // the interesting main JSONObject is on the first position 
     // of the nested array : 0
     JSONObject interestingJSONObject = nestedArray.getJSONObject(0);
    

    所以现在我们需要“additionnalData”Json对象中的“courseId”:

    String courseId = interestingJSONObject.getJSONObject("additionalData").getString("courseId");
    

    就这样!