从SpringJava服务调用解析JSON对象
我从一些服务电话中得到以下回复。我正在尝试解析JSON。实际上,我是JAVA新手,不知道如何解析HTTP调用返回的JSON对象。我得到以下错误:从SpringJava服务调用解析JSON对象,java,json,spring,Java,Json,Spring,我从一些服务电话中得到以下回复。我正在尝试解析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!/:n
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]
答复:
[
"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输入,以便我们能够为您提供有关如何检索所需值的见解
此外,我建议您查看lib,以便非常轻松地将JSON对象直接解析为javapojo。该链接是一个很好的教程,可以帮助您从这里开始。此外,Spring中已经包含了jackson,因此您实际上没有任何要安装的内容
编辑
我误读了JSON输入:我在列表后面看到了一个:而不是一个
因此,您的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对象中的courseId:
String courseId = interestingJSONObject.getJSONObject("additionalData").getString("courseId");
就这样 你说不能是什么意思?这是非常模糊的。这就是我面临这个问题的原因。我从服务调用中得到这个响应,我只需要从additionalDataSure获取couseId,但请更新您的JSON输入,以便我们可以帮助您解决这个问题!我收到错误:org.json.JSONException:JSONArray初始值应该是字符串、集合或数组。请用完整的stacktrace@AbhishekAnand基本上是说输入响应不是您提交的JSON数组。您能检查一下HTTP调用的响应是否给出了您在问题中输入的JSON响应吗?请在'new JSONArrayresp'之前先做一个System.out.printlresp'。
String courseId = interestingJSONObject.getJSONObject("additionalData").getString("courseId");