gpt4 book ai didi

java - 从 Spring JAVA 服务调用中解析 JSON 对象

转载 作者:行者123 更新时间:2023-12-02 10:53:21 25 4
gpt4 key购买 nike

我从某个服务调用中收到以下响应。我正在尝试解析 JSON 。我实际上是 JAVA 新手,不确定如何解析从 HTTP 调用返回的 JSON 对象。我收到以下错误:

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"
}
}]
]

最佳答案

首先,你的 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 对象解析为 JAVA POJO 的 lib 非常容易。该链接是一个很好的教程,可以帮助您从这里开始。此外,jackson 已包含在 Spring 中,因此您实际上无需安装任何内容。

编辑

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

所以你的 JSON 是一个正确的 JSON,但它是一个非常不常见的 JSON,因为它是松散类型的,因此不能使用标准 Jackson 库轻松解析。事实上,在主数组中,字符串与 Json 对象放在一起,这是一种非常糟糕的做法,但这不是你的错,因为我认为你不对该 HTTP 调用的输出负责。

那么你怎样才能真正获得你的值(value)呢?好吧,让我们来描述一下 JSON,这里有一个 JSON 数组,其中包含一个 String 和另一个子 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");

就这样吧!

关于java - 从 Spring JAVA 服务调用中解析 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51988548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com