1

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

Matthew
  • 139
  • 1
  • 10

4 Answers4

3

If you are struggling to use JSON then it may be worth parsing the JSON into a POJO. Whilst you can just use JSON correctly it may be better to do it this way especially if you plan to use more data.

First of all create your POJO classes. Rename them as required.

public class SomeClass
{
    private String offset;

    private String timezone;

    private String latitude;

    private Daily daily;

    private String longitude;

    public String getOffset ()
    {
        return offset;
    }

    public void setOffset (String offset)
    {
        this.offset = offset;
    }

    public String getTimezone ()
    {
        return timezone;
    }

    public void setTimezone (String timezone)
    {
        this.timezone = timezone;
    }

    public String getLatitude ()
    {
        return latitude;
    }

    public void setLatitude (String latitude)
    {
        this.latitude = latitude;
    }

    public Daily getDaily ()
    {
        return daily;
    }

    public void setDaily (Daily daily)
    {
        this.daily = daily;
    }

    public String getLongitude ()
    {
        return longitude;
    }

    public void setLongitude (String longitude)
    {
        this.longitude = longitude;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [offset = "+offset+", timezone = "+timezone+", latitude = "+latitude+", daily = "+daily+", longitude = "+longitude+"]";
    }
}

**

public class Daily
{
    private Data[] data;

    public Data[] getData ()
    {
        return data;
    }

    public void setData (Data[] data)
    {
        this.data = data;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [data = "+data+"]";
    }
}

**

public class Data
{
    private String windGust;

    private String apparentTemperatureMinTime;

    private String temperatureMax;

    private String icon;

    private String precipIntensityMax;

    private String windBearing;

    private String ozone;

    private String temperatureMaxTime;

    private String apparentTemperatureMin;

    private String sunsetTime;

    private String temperatureLow;

    private String precipType;

    private String humidity;

    private String moonPhase;

    private String windSpeed;

    private String apparentTemperatureLowTime;

    private String sunriseTime;

    private String apparentTemperatureLow;

    private String summary;

    private String precipProbability;

    private String temperatureHighTime;

    private String visibility;

    private String precipIntensity;

    private String cloudCover;

    private String temperatureMin;

    private String apparentTemperatureHighTime;

    private String pressure;

    private String dewPoint;

    private String temperatureMinTime;

    private String uvIndexTime;

    private String apparentTemperatureMax;

    private String temperatureHigh;

    private String temperatureLowTime;

    private String apparentTemperatureHigh;

    private String time;

    private String precipIntensityMaxTime;

    private String windGustTime;

    private String uvIndex;

    private String apparentTemperatureMaxTime;

    public String getWindGust ()
    {
        return windGust;
    }

    public void setWindGust (String windGust)
    {
        this.windGust = windGust;
    }

    public String getApparentTemperatureMinTime ()
    {
        return apparentTemperatureMinTime;
    }

    public void setApparentTemperatureMinTime (String apparentTemperatureMinTime)
    {
        this.apparentTemperatureMinTime = apparentTemperatureMinTime;
    }

    public String getTemperatureMax ()
    {
        return temperatureMax;
    }

    public void setTemperatureMax (String temperatureMax)
    {
        this.temperatureMax = temperatureMax;
    }

    public String getIcon ()
    {
        return icon;
    }

    public void setIcon (String icon)
    {
        this.icon = icon;
    }

    public String getPrecipIntensityMax ()
    {
        return precipIntensityMax;
    }

    public void setPrecipIntensityMax (String precipIntensityMax)
    {
        this.precipIntensityMax = precipIntensityMax;
    }

    public String getWindBearing ()
    {
        return windBearing;
    }

    public void setWindBearing (String windBearing)
    {
        this.windBearing = windBearing;
    }

    public String getOzone ()
    {
        return ozone;
    }

    public void setOzone (String ozone)
    {
        this.ozone = ozone;
    }

    public String getTemperatureMaxTime ()
    {
        return temperatureMaxTime;
    }

    public void setTemperatureMaxTime (String temperatureMaxTime)
    {
        this.temperatureMaxTime = temperatureMaxTime;
    }

    public String getApparentTemperatureMin ()
    {
        return apparentTemperatureMin;
    }

    public void setApparentTemperatureMin (String apparentTemperatureMin)
    {
        this.apparentTemperatureMin = apparentTemperatureMin;
    }

    public String getSunsetTime ()
    {
        return sunsetTime;
    }

    public void setSunsetTime (String sunsetTime)
    {
        this.sunsetTime = sunsetTime;
    }

    public String getTemperatureLow ()
    {
        return temperatureLow;
    }

    public void setTemperatureLow (String temperatureLow)
    {
        this.temperatureLow = temperatureLow;
    }

    public String getPrecipType ()
    {
        return precipType;
    }

    public void setPrecipType (String precipType)
    {
        this.precipType = precipType;
    }

    public String getHumidity ()
    {
        return humidity;
    }

    public void setHumidity (String humidity)
    {
        this.humidity = humidity;
    }

    public String getMoonPhase ()
    {
        return moonPhase;
    }

    public void setMoonPhase (String moonPhase)
    {
        this.moonPhase = moonPhase;
    }

    public String getWindSpeed ()
    {
        return windSpeed;
    }

    public void setWindSpeed (String windSpeed)
    {
        this.windSpeed = windSpeed;
    }

    public String getApparentTemperatureLowTime ()
    {
        return apparentTemperatureLowTime;
    }

    public void setApparentTemperatureLowTime (String apparentTemperatureLowTime)
    {
        this.apparentTemperatureLowTime = apparentTemperatureLowTime;
    }

    public String getSunriseTime ()
    {
        return sunriseTime;
    }

    public void setSunriseTime (String sunriseTime)
    {
        this.sunriseTime = sunriseTime;
    }

    public String getApparentTemperatureLow ()
    {
        return apparentTemperatureLow;
    }

    public void setApparentTemperatureLow (String apparentTemperatureLow)
    {
        this.apparentTemperatureLow = apparentTemperatureLow;
    }

    public String getSummary ()
    {
        return summary;
    }

    public void setSummary (String summary)
    {
        this.summary = summary;
    }

    public String getPrecipProbability ()
    {
        return precipProbability;
    }

    public void setPrecipProbability (String precipProbability)
    {
        this.precipProbability = precipProbability;
    }

    public String getTemperatureHighTime ()
    {
        return temperatureHighTime;
    }

    public void setTemperatureHighTime (String temperatureHighTime)
    {
        this.temperatureHighTime = temperatureHighTime;
    }

    public String getVisibility ()
    {
        return visibility;
    }

    public void setVisibility (String visibility)
    {
        this.visibility = visibility;
    }

    public String getPrecipIntensity ()
    {
        return precipIntensity;
    }

    public void setPrecipIntensity (String precipIntensity)
    {
        this.precipIntensity = precipIntensity;
    }

    public String getCloudCover ()
    {
        return cloudCover;
    }

    public void setCloudCover (String cloudCover)
    {
        this.cloudCover = cloudCover;
    }

    public String getTemperatureMin ()
    {
        return temperatureMin;
    }

    public void setTemperatureMin (String temperatureMin)
    {
        this.temperatureMin = temperatureMin;
    }

    public String getApparentTemperatureHighTime ()
    {
        return apparentTemperatureHighTime;
    }

    public void setApparentTemperatureHighTime (String apparentTemperatureHighTime)
    {
        this.apparentTemperatureHighTime = apparentTemperatureHighTime;
    }

    public String getPressure ()
    {
        return pressure;
    }

    public void setPressure (String pressure)
    {
        this.pressure = pressure;
    }

    public String getDewPoint ()
    {
        return dewPoint;
    }

    public void setDewPoint (String dewPoint)
    {
        this.dewPoint = dewPoint;
    }

    public String getTemperatureMinTime ()
    {
        return temperatureMinTime;
    }

    public void setTemperatureMinTime (String temperatureMinTime)
    {
        this.temperatureMinTime = temperatureMinTime;
    }

    public String getUvIndexTime ()
    {
        return uvIndexTime;
    }

    public void setUvIndexTime (String uvIndexTime)
    {
        this.uvIndexTime = uvIndexTime;
    }

    public String getApparentTemperatureMax ()
    {
        return apparentTemperatureMax;
    }

    public void setApparentTemperatureMax (String apparentTemperatureMax)
    {
        this.apparentTemperatureMax = apparentTemperatureMax;
    }

    public String getTemperatureHigh ()
    {
        return temperatureHigh;
    }

    public void setTemperatureHigh (String temperatureHigh)
    {
        this.temperatureHigh = temperatureHigh;
    }

    public String getTemperatureLowTime ()
    {
        return temperatureLowTime;
    }

    public void setTemperatureLowTime (String temperatureLowTime)
    {
        this.temperatureLowTime = temperatureLowTime;
    }

    public String getApparentTemperatureHigh ()
    {
        return apparentTemperatureHigh;
    }

    public void setApparentTemperatureHigh (String apparentTemperatureHigh)
    {
        this.apparentTemperatureHigh = apparentTemperatureHigh;
    }

    public String getTime ()
    {
        return time;
    }

    public void setTime (String time)
    {
        this.time = time;
    }

    public String getPrecipIntensityMaxTime ()
    {
        return precipIntensityMaxTime;
    }

    public void setPrecipIntensityMaxTime (String precipIntensityMaxTime)
    {
        this.precipIntensityMaxTime = precipIntensityMaxTime;
    }

    public String getWindGustTime ()
    {
        return windGustTime;
    }

    public void setWindGustTime (String windGustTime)
    {
        this.windGustTime = windGustTime;
    }

    public String getUvIndex ()
    {
        return uvIndex;
    }

    public void setUvIndex (String uvIndex)
    {
        this.uvIndex = uvIndex;
    }

    public String getApparentTemperatureMaxTime ()
    {
        return apparentTemperatureMaxTime;
    }

    public void setApparentTemperatureMaxTime (String apparentTemperatureMaxTime)
    {
        this.apparentTemperatureMaxTime = apparentTemperatureMaxTime;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [windGust = "+windGust+", apparentTemperatureMinTime = "+apparentTemperatureMinTime+", temperatureMax = "+temperatureMax+", icon = "+icon+", precipIntensityMax = "+precipIntensityMax+", windBearing = "+windBearing+", ozone = "+ozone+", temperatureMaxTime = "+temperatureMaxTime+", apparentTemperatureMin = "+apparentTemperatureMin+", sunsetTime = "+sunsetTime+", temperatureLow = "+temperatureLow+", precipType = "+precipType+", humidity = "+humidity+", moonPhase = "+moonPhase+", windSpeed = "+windSpeed+", apparentTemperatureLowTime = "+apparentTemperatureLowTime+", sunriseTime = "+sunriseTime+", apparentTemperatureLow = "+apparentTemperatureLow+", summary = "+summary+", precipProbability = "+precipProbability+", temperatureHighTime = "+temperatureHighTime+", visibility = "+visibility+", precipIntensity = "+precipIntensity+", cloudCover = "+cloudCover+", temperatureMin = "+temperatureMin+", apparentTemperatureHighTime = "+apparentTemperatureHighTime+", pressure = "+pressure+", dewPoint = "+dewPoint+", temperatureMinTime = "+temperatureMinTime+", uvIndexTime = "+uvIndexTime+", apparentTemperatureMax = "+apparentTemperatureMax+", temperatureHigh = "+temperatureHigh+", temperatureLowTime = "+temperatureLowTime+", apparentTemperatureHigh = "+apparentTemperatureHigh+", time = "+time+", precipIntensityMaxTime = "+precipIntensityMaxTime+", windGustTime = "+windGustTime+", uvIndex = "+uvIndex+", apparentTemperatureMaxTime = "+apparentTemperatureMaxTime+"]";
    }
}

You can convert Data[] to an ArrayList which makes it easier to work with if you wanted.

Now you want to convert your JSON into a POJO.

To do that we will use GSON, add this to your app.gradle file

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'

And now to convert the JSON, write code like this:

Gson gson = new Gson();
SomeClass myClass = gson.fromJson(jsonString, SomeClass.class);

Now all you would need to do is

setText(myClass.getDaily().getDaily()[0].getSummary();

All that you would need to do it fix this

getDaily()[0]

to check that data actually exists or it could throw an exception.

apmartin1991
  • 3,064
  • 1
  • 23
  • 44
2

In your JSON String data is not the root node but is inside daily node. So firstly you have to parse the daily node then get the data array. Try doing the following.

private void jsonParse() {

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

    try {
        // Reading the main JSON.
        JSONObject jsonObject = new JSONObject(jsonRawData);

        // Reading the content from daily node of JSON to get data array
        JSONObject jsonData = jsonObject.getJSONObject("daily")

        // Reading data node to get desired array
        JSONArray jsonArray = jsonData.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();
    }

}
Nitesh
  • 3,868
  • 1
  • 20
  • 26
1

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

        }
Koushik Mondal
  • 865
  • 7
  • 15
0

try add this line before getting your json array

JSONObject jsonObject = new JSONObject(jsonRawData);
JSONObject jsonData = jsonObject.getJSONObject("daily")
JSONArray jsonArray = jsonData.getJSONArray("data");
Wassim Ben Hssen
  • 519
  • 6
  • 23