0

I'm making an android app to control my AC, in my app i would like to know the temperature outside. I have an json link to the local weather forecast provider and it have the temperature value I'm looking for.

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

My problem is that I don't know how to get to the temperature value when it´s inside several arrays. The object I'm looking for is inside "timeSeries" -> "parameters" -> and the name is "t" and it is that "value" I want (it´s the temperature in Celsius).

I have tried several ways of fix it but obvious I'm not there :). I insert a part of my code so you can see what I'm trying.

        @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;
        }
    });

It´s in the getCurrentDetails i want to get the temperature and then return it.

Jay
  • 1
  • You should use the Gson library which will allow you to deserialize the JSON to a complex object and simply access the values your need via exposed properties. See the accepted answer here: https://stackoverflow.com/a/38136089/2754727 – pnavk Jan 31 '19 at 23:27
  • Thank you guys for the answer, I will definitely have a look on that! – Jay Feb 01 '19 at 08:08

1 Answers1

0
  1. If you want to get the latest temperature from the json data

In getCurrentDetails() method:

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. If you want to get all temperatures in the jsonData

In getCurrentDetails() method:

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.
egbinola
  • 21
  • 4
  • Thank you for the answer and the both examples, it worked great. I edited the parameters.getJsonObject(0) to index 11 to get the value I was interested in. Cheers – Jay Feb 01 '19 at 08:11
  • @Jay.... So, I have modified the code.. Instead of you hard-coding the most recent temperature using **parameters.getJsonObject(11);** , consider a scenario where the number of parameters grow beyond " 11".. I'll suggest you use this for a more robust code: **parameters.getJsonObject(parameters.length() - 1);** – egbinola Feb 01 '19 at 21:07