How can I parse a JSON string in which an array is not comma separated? (Gson, Android Studio)

huangapple go评论86阅读模式
英文:

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:

{ &quot;_embedded&quot;:{
      &quot;events&quot;:[
         {
            &quot;name&quot;:&quot;Hamilton&quot;,
            &quot;type&quot;:&quot;event&quot;,
            &quot;id&quot;:&quot;Z7r9jZ1Ae0EP8&quot;,
            &quot;test&quot;:false,
            &quot;url&quot;:&quot;http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950&quot;,
            &quot;_embedded&quot;:{
               &quot;venues&quot;:{
                  &quot;0&quot;:                  {
                     &quot;name&quot;:&quot;Reynolds Hall&quot;,
                     &quot;type&quot;:&quot;venue&quot;,
                     &quot;id&quot;:&quot;Z7r9jZadyb&quot;,
                     &quot;test&quot;:false,
                     &quot;locale&quot;:&quot;en-us&quot;,
                     &quot;location&quot;:{
                        &quot;longitude&quot;:&quot;-115.162598&quot;,
                        &quot;latitude&quot;:&quot;36.182201&quot;
                     }
                  }
               }
            }
         }
      ]
   }
}

More venues items should look:

{
   &quot;_embedded&quot;:{
      &quot;events&quot;:[
         {
            &quot;name&quot;:&quot;Hamilton&quot;,
            &quot;type&quot;:&quot;event&quot;,
            &quot;id&quot;:&quot;Z7r9jZ1Ae0EP8&quot;,
            &quot;test&quot;:false,
            &quot;url&quot;:&quot;http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2927950&quot;,
            &quot;_embedded&quot;:{
               &quot;venues&quot;:{
                  &quot;0&quot;:{
                     &quot;name&quot;:&quot;Reynolds Hall&quot;,
                     &quot;type&quot;:&quot;venue&quot;,
                     &quot;id&quot;:&quot;Z7r9jZadyb&quot;,
                     &quot;test&quot;:false,
                     &quot;locale&quot;:&quot;en-us&quot;,
                     &quot;location&quot;:{
                        &quot;longitude&quot;:&quot;-115.162598&quot;,
                        &quot;latitude&quot;:&quot;36.182201&quot;
                     }
                  },
                  
                  &quot;1&quot;:{
                     &quot;name&quot;:&quot;Reynolds Hall&quot;,
                     &quot;type&quot;:&quot;venue&quot;,
                     &quot;id&quot;:&quot;Z7r9jZadyb&quot;,
                     &quot;test&quot;:false,
                     &quot;locale&quot;:&quot;en-us&quot;,
                     &quot;location&quot;:{
                        &quot;longitude&quot;:&quot;-115.162598&quot;,
                        &quot;latitude&quot;:&quot;36.182201&quot;
                     }
                  }
                  
                  
               }
            }
         }
      ]
   }
}

Than you can deserialize venues as Map<Integer,Venue>, where keys will be 0,1 and so on.

huangapple
  • 本文由 发表于 2020年8月19日 17:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63483496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定