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"
}
}
]
}
}
]
}
}
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();
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.