英文:
How can I parse a JSON string in which an array is not comma separated? (Gson, Android Studio)
问题
这是 JSON 格式(来自 API 的响应 - https://developer.ticketmaster.com/api-explorer/v2/)。如果我为 Event 类添加“_embedded”(其中包含场馆列表)作为属性,则不起作用。
我应该如何从这个 JSON 中提取位置?
{
"_embedded":{
"events":[
{
"name":"Hamilton",
"type":"event",
"id":"Z7r9jZ1Ae0EP8",
"test":false,
"url":"http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950",
"_embedded":{
"venues":[
{
"name":"Reynolds Hall",
"type":"venue",
"id":"Z7r9jZadyb",
"test":false,
"locale":"en-us",
"location":{
"longitude":"-115.162598",
"latitude":"36.182201"
}
}
]
}
}
]
}
}
英文:
This is the JSON format (response from API - https://developer.ticketmaster.com/api-explorer/v2/). If I add "_embedded"(which have a list of venues) as attribute for Event Class doesn't work.
How can I take the location from this JSON?
{ "_embedded":{
"events":[
{
"name":"Hamilton",
"type":"event",
"id":"Z7r9jZ1Ae0EP8",
"test":false,
"url":"http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950",
"_embedded":{
"venues":[
"0": {
"name":"Reynolds Hall",
"type":"venue",
"id":"Z7r9jZadyb",
"test":false,
"locale":"en-us",
"location":{
"longitude":"-115.162598",
"latitude":"36.182201"
}
}
]
}
}
]
}
}
答案1
得分: 0
以下是翻译好的内容:
如果这是您的 JSON:
{
"_embedded": {
"events": [
{
"name": "Hamilton",
"type": "event",
"id": "Z7r9jZ1Ae0EP8",
"test": false,
"url": "http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950",
"_embedded": {
"venues": [
{
"name": "Reynolds Hall",
"type": "venue",
"id": "Z7r9jZadyb",
"test": false,
"locale": "en-us",
"location": {
"longitude": "-115.162598",
"latitude": "36.182201"
}
}
]
}
}
]
}
}
然后您可以像这样检索位置(假设您将其存储在名为 jString 的变量中):
try {
JSONObject jObj = new JSONObject(jString);
String lat = jObj
.getJSONObject("_embedded")
.getJSONArray("events")
.getJSONObject(0)
.getJSONObject("_embedded")
.getJSONArray("venues")
.getJSONObject(0)
.getJSONObject("location")
.getString("latitude");
String lng = jObj
.getJSONObject("_embedded")
.getJSONArray("events")
.getJSONObject(0)
.getJSONObject("_embedded")
.getJSONArray("venues")
.getJSONObject(0)
.getJSONObject("location")
.getString("longitude");
LatLng location = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
} catch (JSONException | NumberFormatException e) {
e.printStackTrace();
}
英文:
If this is your JSON:
{
"_embedded": {
"events": [
{
"name": "Hamilton",
"type": "event",
"id": "Z7r9jZ1Ae0EP8",
"test": false,
"url": "http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950",
"_embedded": {
"venues": [
{
"name": "Reynolds Hall",
"type": "venue",
"id": "Z7r9jZadyb",
"test": false,
"locale": "en-us",
"location": {
"longitude": "-115.162598",
"latitude": "36.182201"
}
}
]
}
}
]
}
}
Then you can retrieve location like this (assuming that you stored it in jString variable) :
try {
JSONObject jObj = new JSONObject(jString);
String lat = jObj
.getJSONObject("_embedded")
.getJSONObject("events")
.getJSONObject("_embedded")
.getJSONArray("venues")
.getJSONObject(0)
.getJSONObject("location")
.getString("latitude");
String lng = jObj
.getJSONObject("_embedded")
.getJSONObject("events")
.getJSONObject("_embedded")
.getJSONArray("venues")
.getJSONObject(0)
.getJSONObject("location")
.getString("longitude");
LatLng location = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
} catch (JSONException | NumberFormatException e) {
e.printStackTrace();
}
答案2
得分: 0
以下是翻译好的内容:
你不能这样做,因为那不是有效的 JSON,尝试任何验证器,例如:https://jsonformatter.curiousconcept.com/#
所以基本上,它不是 JSON。它只是类似于 JSON。参考:https://www.rfc-editor.org/rfc/rfc8259
在处理之前,您可以尝试进行更正。在这种情况下,将 [
替换为 {
,应该看起来像这样:
{ "_embedded":{
"events":[
{
"name":"Hamilton",
"type":"event",
"id":"Z7r9jZ1Ae0EP8",
"test":false,
"url":"http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950",
"_embedded":{
"venues":{
"0": {
"name":"Reynolds Hall",
"type":"venue",
"id":"Z7r9jZadyb",
"test":false,
"locale":"en-us",
"location":{
"longitude":"-115.162598",
"latitude":"36.182201"
}
}
}
}
}
]
}
}
更多的 venues
项应该如下所示:
{
"_embedded":{
"events":[
{
"name":"Hamilton",
"type":"event",
"id":"Z7r9jZ1Ae0EP8",
"test":false,
"url":"http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950",
"_embedded":{
"venues":{
"0":{
"name":"Reynolds Hall",
"type":"venue",
"id":"Z7r9jZadyb",
"test":false,
"locale":"en-us",
"location":{
"longitude":"-115.162598",
"latitude":"36.182201"
}
},
"1":{
"name":"Reynolds Hall",
"type":"venue",
"id":"Z7r9jZadyb",
"test":false,
"locale":"en-us",
"location":{
"longitude":"-115.162598",
"latitude":"36.182201"
}
}
}
}
}
]
}
}
然后您可以将 venues
反序列化为 Map<Integer, Venue>
,其中键将为 0、1 等等。
英文:
You can't, becasue that's not valid json, try any validator, for example: https://jsonformatter.curiousconcept.com/#
So basically, it's not json. It's only similar to json. Reference: https://www.rfc-editor.org/rfc/rfc8259
You can try to correct it before processing. In this case, replace [
with {
, so it should look like this:
{ "_embedded":{
"events":[
{
"name":"Hamilton",
"type":"event",
"id":"Z7r9jZ1Ae0EP8",
"test":false,
"url":"http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950",
"_embedded":{
"venues":{
"0": {
"name":"Reynolds Hall",
"type":"venue",
"id":"Z7r9jZadyb",
"test":false,
"locale":"en-us",
"location":{
"longitude":"-115.162598",
"latitude":"36.182201"
}
}
}
}
}
]
}
}
More venues
items should look:
{
"_embedded":{
"events":[
{
"name":"Hamilton",
"type":"event",
"id":"Z7r9jZ1Ae0EP8",
"test":false,
"url":"http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950",
"_embedded":{
"venues":{
"0":{
"name":"Reynolds Hall",
"type":"venue",
"id":"Z7r9jZadyb",
"test":false,
"locale":"en-us",
"location":{
"longitude":"-115.162598",
"latitude":"36.182201"
}
},
"1":{
"name":"Reynolds Hall",
"type":"venue",
"id":"Z7r9jZadyb",
"test":false,
"locale":"en-us",
"location":{
"longitude":"-115.162598",
"latitude":"36.182201"
}
}
}
}
}
]
}
}
Than you can deserialize venues
as Map<Integer,Venue>, where keys will be 0,1 and so on.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论