java - 从服务调用 Spring JAVA 解析 JSON 对象
问题描述
我从一些服务电话中得到以下响应。我正在尝试解析 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。该链接是一个很好的教程,可以帮助您从这里开始。此外,jackson 已经包含在 Spring 中,因此您实际上无需安装任何东西。
编辑
我误读了 JSON 输入:我看到的是:
after"list"
而不是,
.
所以你的 JSON 是一个正确的 JSON,但它是一个非常不常见的 JSON,因为它的类型很松散,因此不能用标准的 Jackson 库轻松解析。事实上,在主数组中,一个字符串与一个 Json 对象放在一起,这是一种非常糟糕的做法,但这不是你的错,因为我认为你不对这个 HTTP 调用的输出负责。
那么你如何才能真正获得你的价值呢?好吧,让我们来描述一下 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 Object 的“courseId”:
String courseId = interestingJSONObject.getJSONObject("additionalData").getString("courseId");
你去吧!
推荐阅读
- web - 在不打开浏览器的情况下执行 php 脚本
- python-3.x - 在python中的csv文件行上实现插入排序
- android - Exoplayer 音频播放背景,甚至视频已关闭
- laravel - 在视图 laravel 5.7 中访问预先加载的对象
- javascript - Heroku 应用程序错误:应用程序发生错误,无法提供您的页面
- python - 如何从python中的字符串值中获取字典对象
- php - Composer install Laravel:您的需求无法解析为一组可安装的软件包
- azure - 使用子网 ID 的引用创建 AKS 群集时出错
- sql - 调整 Postgres 查询
- sql-server - Microsoft SQL Server 编辑导入的文件