【问题标题】:How to get specific value from a JSON string? Java/Android [duplicate]如何从 JSON 字符串中获取特定值? Java/Android [重复]
【发布时间】:2019-10-08 17:46:18
【问题描述】:

我正在 android-studio 开发一个项目。但我坚持这一点。我已经向 Okhttp3 发出了请求,这将返回一个 Response 对象。我可以将响应作为字符串(json 文件)检索。

MainActivity.java

client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        if(response.isSuccessful()) {
            String data = response.body().string();
            JsonParser parser = new JsonParser();

            try {
                JsonObject obj = (JsonObject) parser.parse(data);
                String ad = obj.get("features").toString();
                Log.d("msg", data);
                Log.d("msg", ad);
                Log.d("msg", "Works");
            } catch (Exception e) {
                Log.d("msg", "Error");
            }

回应

2019-05-22 10:55:12.785 1301-1631/com.example.triptracker D/msg: {"type":"FeatureCollection","query":[4.35822,51.91962],"features":[{"id":"address.2630076086694170","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"point"},"text":"Dotterbloemstraat","place_name":"Dotterbloemstraat 12c, 3135 Vlaardingen, Netherlands","center":[4.358192,51.919666],"geometry":{"type":"Point","coordinates":[4.358192,51.919666]},"address":"12c","context":[{"id":"postcode.7757734261857840","text":"3135"},{"id":"place.6763396954136802","wikidata":"Q210007","text":"Vlaardingen"},{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"postcode.7757734261857840","type":"Feature","place_type":["postcode"],"relevance":1,"properties":{},"text":"3135","place_name":"3135, Vlaardingen, Zuid-Holland, Netherlands","bbox":[4.340678,51.909008,4.36899,51.925254],"center":[4.35,51.92],"geometry":{"type":"Point","coordinates":[4.35,51.92]},"context":[{"id":"place.6763396954136802","wikidata":"Q210007","text":"Vlaardingen"},{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"place.6763396954136802","type":"Feature","place_type":["place"],"relevance":1,"properties":{"wikidata":"Q210007"},"text":"Vlaardingen","place_name":"Vlaardingen, Zuid-Holland, Netherlands","bbox":[4.270188,51.896102,4.369914,51.951478],"center":[4.35,51.91667],"geometry":{"type":"Point","coordinates":[4.35,51.91667]},"context":[{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"region.8599455180798270","type":"Feature","place_type":["region"],"relevance":1,"properties":{"short_code":"NL-ZH","wikidata":"Q694"},"text":"Zuid-Holland","place_name":"Zuid-Holland, Netherlands","bbox":[3.7235244,51.64378,5.150749,52.390802],"center":[4.66667,52],"geometry":{"type":"Point","coordinates":[4.66667,52]},"context":[{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"country.9349515904622050","type":"Feature","place_type":["country"],"relevance":1,"properties":{"short_code":"nl","wikidata":"Q55"},"text":"Netherlands","place_name":"Netherlands","bbox":[3.1862592,50.750667,7.230902,53.665238],"center":[5.55,52.31667],"geometry":{"type":"Point","coordinates":[5.55,52.31667]}}],"attribution":"NOTICE: © 2019 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."}
2019-05-22 10:55:12.786 1301-1631/com.example.triptracker D/msg: [{"id":"address.2630076086694170","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"point"},"text":"Dotterbloemstraat","place_name":"Dotterbloemstraat 12c, 3135 Vlaardingen, Netherlands","center":[4.358192,51.919666],"geometry":{"type":"Point","coordinates":[4.358192,51.919666]},"address":"12c","context":[{"id":"postcode.7757734261857840","text":"3135"},{"id":"place.6763396954136802","wikidata":"Q210007","text":"Vlaardingen"},{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"postcode.7757734261857840","type":"Feature","place_type":["postcode"],"relevance":1,"properties":{},"text":"3135","place_name":"3135, Vlaardingen, Zuid-Holland, Netherlands","bbox":[4.340678,51.909008,4.36899,51.925254],"center":[4.35,51.92],"geometry":{"type":"Point","coordinates":[4.35,51.92]},"context":[{"id":"place.6763396954136802","wikidata":"Q210007","text":"Vlaardingen"},{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"place.6763396954136802","type":"Feature","place_type":["place"],"relevance":1,"properties":{"wikidata":"Q210007"},"text":"Vlaardingen","place_name":"Vlaardingen, Zuid-Holland, Netherlands","bbox":[4.270188,51.896102,4.369914,51.951478],"center":[4.35,51.91667],"geometry":{"type":"Point","coordinates":[4.35,51.91667]},"context":[{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"region.8599455180798270","type":"Feature","place_type":["region"],"relevance":1,"properties":{"short_code":"NL-ZH","wikidata":"Q694"},"text":"Zuid-Holland","place_name":"Zuid-Holland, Netherlands","bbox":[3.7235244,51.64378,5.150749,52.390802],"center":[4.66667,52],"geometry":{"type":"Point","coordinates":[4.66667,52]},"context":[{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"country.9349515904622050","type":"Feature","place_type":["country"],"relevance":1,"properties":{"short_code":"nl","wikidata":"Q55"},"text":"Netherlands","place_name":"Netherlands","bbox":[3.1862592,50.750667,7.230902,53.665238],"center":[5.55,52.31667],"geometry":{"type":"Point","coordinates":[5.55,52.31667]}}]
2019-05-22 10:55:12.786 1301-1631/com.example.triptracker D/msg: Works

但我需要的是嵌套在“功能”中的“地点名称”。我已经尝试了很多,但仍然不知道如何访问“place_name”。在 JavaScript 中,它像 features[0].place_name 一样简单得多。但在 Java 中,这对我来说是未知的。有人可以帮忙吗?

我想要的结果:

"Dotterbloemstraat 12c, 3135 Vlaardingen, Netherlands"

【问题讨论】:

  • 您能向我们展示您创建的模型吗?
  • 将您的 json 响应转换为您的模态类。我建议您请改用 retofit 库 http square.github.io/retrofit
  • 1) 您正在使用没有改造的 OkHttp。 2)您是在随机猜测代码,而不是阅读如何正确解析 json 字符串。要么实现 Retrofit square.github.io/retrofit 要么阅读 json parsing tutorialspoint.com/android/android_json_parser.htm
  • 试试这个 - JSONArray array = obj.getJSONArray("features"); JSONObject main_detail = array.getJSONObject(0); String place_name = main_detail.getString("place_name");

标签: java android json gson okhttp3


【解决方案1】:

你应该得到 Json Array 的特性

JsonArray ad = obj.getAsJsonArray("features");
JsonObject ob = ad.get(0).getAsJsonObject();
String placeName = ob.get("place_name").getAsString()

【讨论】:

  • 这给了我以下错误:错误:类 JsonElement 中的方法 getAsJsonObject 不能应用于给定类型;必需:未找到参数:int 原因:实际参数列表和形式参数列表的长度不同
  • 我编辑了,试试新版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多