首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用数组名称中的破折号/连字符解析JSON

无法使用数组名称中的破折号/连字符解析JSON
EN

Stack Overflow用户
提问于 2020-12-26 00:53:28
回答 2查看 180关注 0票数 0

我正在使用来解析一个名为“附近城市”的json数组。这是json

代码语言:javascript
复制
 {
  "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代码:

代码语言:javascript
复制
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");
                            }

错误:附近的城市没有价值

好像他不喜欢破折号/连字符(-)。我怎么才能解决这个问题?

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

编辑:当我得到原点数组:products.getJsonArray("origin")时,它就能工作了,所以我确定问题就在破折号上。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-26 11:56:30

好吧,所以我发现了问题。附近的城市数组只出现在我名单上的第一次地震中。其他地震不包含任何附近的城市数组,所以在我的googleMap中,我单击一个表示地震的标记,它不确定我是否单击了列表中的第一个地震。(我的名单上有30次地震,所以.)

编辑:有超过一个最后。因此,我保留了这段代码,并在catch {}中添加了一个祝酒词,它显示“没有更多的详细信息”,而在附近--城市不存在。

票数 0
EN

Stack Overflow用户

发布于 2020-12-26 02:30:57

你的代码:

代码语言:javascript
复制
     JSONObject nearbyCitiesJson = contentObj.getJSONObject("nearby-cities.json")

在您的示例结构中没有一个名为“附近-cities.json”的字段。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65452922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档