首页 > 解决方案 > 如果 JSON 对象名称包含连字符或破折号 (-),如何在 android studio 中使用 Volley Library 获取类似“附近城市”的 JSON 对象名称?

问题描述

JSONArray nearbyCities = products.getJSONArray("nearby-cities");
JSONObject nearbyCititesObj = content.getJSONObject("nearby-cities.json");

要检索的对象是 properties->products->nearby-cities->contents->nearby-cities.json

这里要检索的问题分别是对象nearby-citiesnearby-cities.json数组。

下面是 json 文件的链接 https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc73408691.geojson

标签: jsonandroid-studioandroid-volley

解决方案


连字符不会有任何区别。您的错误可能在其他地方

response.getJSONObject("properties").getJSONObject("products").getJSONArray("nearby-cities").getJSONObject(0).getJSONObject("contents").getJSONObject("nearby-cities.json").toString();

这一行给出了预期的输出


推荐阅读