无法解析数组名称中包含 dash/hyphen 的 JSON 数组

Cannot parse JSON Array with a dash/hyphen in the array name

我正在使用 Volley 库解析一个名为“nearby-cities”的 json 数组。这是 json

 {
  "type": "Feature",
  "properties": {
    "mag": 0.68,
    "place": "9km NE of Aguanga, CA",
    "time": 1608913545940,
    "updated": 1608916689050,
    "tz": null,
    "url": "https://earthquake.usgs.gov/earthquakes/eventpage/ci39503839",
    "felt": null,
    "cdi": null,
    "mmi": null,
    "alert": null,
    "status": "reviewed",
    "tsunami": 0,
    "sig": 7,
    "net": "ci",
    "code": "39503839",
    "ids": ",ci39503839,",
    "sources": ",ci,",
    "types": ",focal-mechanism,nearby-cities,origin,phase-data,scitech-link,",
    "nst": 29,
    "dmin": 0.01865,
    "rms": 0.15,
    "gap": 35,
    "magType": "ml",
    "type": "earthquake",
    "title": "M 0.7 - 9km NE of Aguanga, CA",
    "products": {
      "focal-mechanism": [
        {}
      ],
      "nearby-cities": [
        {
          "indexid": "10110743",
          "indexTime": 1608916668761,
          "id": "urn:usgs-product:ci:nearby-cities:ci39503839:1608916666997",
          "type": "nearby-cities",
          "code": "ci39503839",
          "source": "ci",
          "updateTime": 1608916666997,
          "status": "UPDATE",
          "properties": {
            "eventsource": "ci",
            "eventsourcecode": "39503839",
            "pdl-client-version": "Version 2.5.1 2020-06-25"
          },
          "preferredWeight": 6,
          "contents": {
            "nearby-cities.json": {
              "contentType": "application/json",
              "lastModified": 1608916666000,
              "length": 618,
              "url": "https://earthquake.usgs.gov/realtime/product/nearby-cities/ci39503839/ci/1608916666997/nearby-cities.json"
            }
          }
        }
      ],
      "origin": [
        {
          "indexid": "10110741",
          "indexTime": 1608916666684,
          "id": "urn:usgs-product:ci:origin:ci39503839:1608916666250",
          "type": "origin",
          "code": "ci39503839",
          "source": "ci",
          "updateTime": 1608916666250,
          "status": "UPDATE",
          "properties": {
            "azimuthal-gap": "35",
            "depth": "4.58",
            "depth-type": "from location",
            "evaluation-status": "final",
            "event-type": "earthquake",
            "eventParametersPublicID": "quakeml:service.scedc.caltech.edu/fdsnws/event/1/query?eventid=39503839",
            "eventsource": "ci",
            "eventsourcecode": "39503839",
            "eventtime": "2020-12-25T16:25:45.940Z",
            "horizontal-error": "0.2",
            "latitude": "33.4946667",
            "longitude": "-116.7931667",
            "magnitude": "0.68",
            "magnitude-azimuthal-gap": "39.8",
            "magnitude-error": "0.145",
            "magnitude-num-stations-used": "26",
            "magnitude-source": "CI",
            "magnitude-type": "ml",
            "minimum-distance": "0.01865",
            "num-phases-used": "40",
            "num-stations-used": "29",
            "origin-source": "CI",
            "pdl-client-version": "Version 2.5.1 2020-06-25",
            "quakeml-magnitude-publicid": "quakeml:service.scedc.caltech.edu/fdsnws/event/1/query?magnitudeid=109651180",
            "quakeml-origin-publicid": "quakeml:service.scedc.caltech.edu/fdsnws/event/1/query?originid=106968340",
            "quakeml-publicid": "quakeml:service.scedc.caltech.edu/fdsnws/event/1/query?eventid=39503839",
            "review-status": "reviewed",
            "standard-error": "0.15",
            "title": "9km NE of Aguanga, CA",
            "version": "4",
            "vertical-error": "0.36"
          }
        }
      ]
    }
  }
}

我的java代码:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        String detailsUrl = "";
                        try {
                            JSONObject properties = response.getJSONObject("properties");

                            JSONObject products = properties.getJSONObject("products");

                            JSONArray nearbyCities = products.getJSONArray("nearby-cities");

                            for (int i =0 ; i<nearbyCities.length(); i++) {
                                JSONObject nearbyCitiesObj = nearbyCities.getJSONObject(i);

                                JSONObject contentObj = nearbyCitiesObj.getJSONObject("contents");

                                JSONObject nearbyCitiesJson = contentObj.getJSONObject("nearby-cities.json");
                                 detailsUrl = nearbyCitiesJson.getString("url");
                            }

错误:附近城市没有价值

好像他不喜欢dash/hyphen(-)。我该如何解决?

Json link : https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ci39503839.geojson

编辑:当我得到原始数组时它起作用了:products.getJsonArray("origin")所以我确定问题出在破折号上。

您的代码:

     JSONObject nearbyCitiesJson = contentObj.getJSONObject("nearby-cities.json")

您的示例结构中没有名为“nearby-cities.json”的字段。

它被称为“附近的城市”。

好的,所以我发现了问题。 nearby-cities 数组仅出现在我列表中的第一次地震中。其他地震不包含任何附近城市数组,因此当在我的 googleMap 中单击代表地震的标记时,不确定我是否单击列表中的第一个地震。 (我的列表中有 30 次地震,所以...)

编辑:终于多了1个。所以我保留了这段代码,并在我的 catch {} 中添加了一个祝酒词,它显示“没有更多详细信息”,而 nearby-cities 不存在。