Search code examples
androidjsonparsingdarksky

Get Value From JSON in Android Studio


I know there are a ton of JSON parsing questions out there. I apologize for asking another one, but I can not get this to work.

I am getting a JSON response from DarkSky. That response is fine and I am putting that data into a TextView called tvForecast. Now I am trying to get, from that string, the value for "summary".

Here is my JSON response:

{"latitude":36.946813,"longitude":127.019736,"timezone":"Asia/Seoul","daily":{"data":[{"time":1570374000,"summary":"Light rain until evening.","icon":"rain","sunriseTime":1570397580,"sunsetTime":1570439340,"moonPhase":0.31,"precipIntensity":0.0327,"precipIntensityMax":0.0838,"precipIntensityMaxTime":1570406520,"precipProbability":1,"precipType":"rain","temperatureHigh":60.89,"temperatureHighTime":1570441560,"temperatureLow":53.41,"temperatureLowTime":1570477080,"apparentTemperatureHigh":60.97,"apparentTemperatureHighTime":1570441860,"apparentTemperatureLow":53.9,"apparentTemperatureLowTime":1570477080,"dewPoint":56.92,"humidity":0.94,"pressure":1019.4,"windSpeed":4.02,"windGust":15.19,"windGustTime":1570374000,"windBearing":96,"cloudCover":0.98,"uvIndex":4,"uvIndexTime":1570418340,"visibility":6.895,"ozone":289,"temperatureMin":55.57,"temperatureMinTime":1570396140,"temperatureMax":63.38,"temperatureMaxTime":1570374000,"apparentTemperatureMin":56.07,"apparentTemperatureMinTime":1570396020,"apparentTemperatureMax":62.88,"apparentTemperatureMaxTime":1570374000}]},"offset":9}

Here is my method to collect this information from one TextView (tvForecast) and put the "summary" value into another TextView (tvSummary).

private void jsonParse() {

        String jsonRawData = tvForecast.getText().toString();

        try {
            JSONObject jsonObject = new JSONObject(jsonRawData);

            JSONArray jsonArray = jsonObject.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject data = jsonArray.getJSONObject(i);

                String summary = data.getString("summary");

                tvSummary.append(summary);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

This is the message from the Logcat. Note the app isn't crashing. It just appears that it's not finding the value that I want.

W/System.err: org.json.JSONException: No value for data


Solution

  • You missed to add "daily" object. Please try this.

    private void jsonParse() {
    
                String jsonRawData = tvForecast.getText().toString();
    
                try {
    
                    JSONObject jsonObject = new JSONObject(jsonRawData);
                    JSONObject jsonDailyObject = jsonObject.getJSONObject("daily")
    
                    JSONArray jsonArray = jsonDailyObject.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject data = jsonArray.getJSONObject(i);
    
                        String summary = data.getString("summary");
    
                        tvSummary.append(summary);
    
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
            }