【问题标题】:How can i get the value that are inside several arrays when getting json data? [duplicate]获取 json 数据时如何获取多个数组中的值? [复制]
【发布时间】:2019-01-31 23:10:30
【问题描述】:

我正在制作一个安卓应用来控制我的空调,在我的应用中我想知道外面的温度。我有一个指向当地天气预报提供商的 json 链接,它具有我正在寻找的温度值。

链接到 JSON (https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/16.158/lat/58.5812/data.json)

我的问题是,当温度值位于多个数组中时,我不知道如何获取它。我要找的对象在“timeSeries”->“parameters”->里面,名字是“t”,就是我想要的那个“值”(它是摄氏温度)。

我已经尝试了几种修复它的方法,但显然我不在那里 :)。我插入了我的代码的一部分,这样你就可以看到我正在尝试什么。

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String forecastData;
            try {
                String jsonData = response.body().string();

                Log.v(TAG, jsonData);
                if (response.isSuccessful()) {

                    forecastData = getCurrentDetails(jsonData);

                }
            } catch (IOException e) {
                Log.e(TAG, "IO Exception caught: ", e);
            } catch (JSONException e) {
                Log.e(TAG, "JSON Exception caught:", e);
            }
        }
        private String getCurrentDetails(String jsonData) throws     JSONException {
            JSONObject forecast = new JSONObject(jsonData);
            JSONArray currently = forecast.getJSONArray("timeSeries");
            String currentTemp = "";
            return currentTemp;
        }
    });

它在 getCurrentDetails 中,我想获取温度然后返回它。

【问题讨论】:

  • 您应该使用 Gson 库,它允许您将 JSON 反序列化为复杂对象,并通过公开的属性简单地访问您需要的值。在此处查看接受的答案:stackoverflow.com/a/38136089/2754727
  • 谢谢大家的回答,我一定会看看的!

标签: android json api


【解决方案1】:
  1. 如果你想从json数据中获取最新的温度

在 getCurrentDetails() 方法中:

JsonObject forecast = new JsonObject(jsonData);
JsonArray timeSeries = forecast.getJsonArray("timeSeries");
JsonObject current = timeSeries.getJsonObject(0);
JsonArray parameters = current.getJsonArray("parameters");
JsonObject firstParam = parameters.getJsonObject(parameters.length() - 1);
JsonArray values = firstParam.getJsonArray("values");
String tempValue = values.getString(0);
//You can now return the tempValue
  1. 如果要获取 jsonData 中的所有温度

在 getCurrentDetails() 方法中:

JsonObject forecast = new JsonObject(jsonData);
JsonArray timeSeries = forecast.getJsonArray("timeSeries");

ArrayList<String> temps = new ArrayList<>();

for(int i=0; i < timeSeries.length(); i++){
JsonObject current = timeSeries.getJsonObject(i);
JsonArray parameters = current.getJsonArray("parameters");
JsonObject firstParam = parameters.getJsonObject(parameters.length() - 1);
JsonArray values = firstParam.getJsonArray("values");
String tempValue = values.getString(0);

temps.add(tempValue);
}
// You can now return the temps.
//NOTE that in this case, your
//getCurrentDetails() method should
//return ArrayList<String> not String.

【讨论】:

  • 感谢您的回答和这两个示例,效果很好。我将 parameters.getJsonObject(0) 编辑到索引 11 以获得我感兴趣的值。干杯
  • @Jay.... 所以,我已经修改了代码.. 而不是你使用 parameters.getJsonObject(11); 硬编码最近的温度,考虑参数数量超过“ 11”的情况。我建议您将其用于更健壮的代码:parameters.getJsonObject(parameters.length() - 1);
猜你喜欢
  • 2015-01-12
  • 1970-01-01
  • 2020-05-18
  • 2018-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
相关资源
最近更新 更多