在 Android Studio 中从 JSON 获取价值

Get Value From JSON in Android Studio

我知道那里有大量 JSON 解析问题。我很抱歉问了另一个,但我无法让它工作。

我收到了来自 DarkSky 的 JSON 回复。该响应很好,我将该数据放入名为 tvForecast 的 TextView 中。现在我试图从该字符串中获取 "summary".

的值

这是我的 JSON 回复:

{"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}

这是我从一个 TextView (tvForecast) 收集此信息并将 "summary" 值放入另一个 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();
        }

    }

这是来自 Logcat 的消息。请注意,该应用程序没有崩溃。它似乎没有找到我想要的值。

W/System.err: org.json.JSONException: 没有数据值

尝试在获取 json 数组之前添加此行

JSONObject jsonObject = new JSONObject(jsonRawData);
JSONObject jsonData = jsonObject.getJSONObject("daily")
JSONArray jsonArray = jsonData.getJSONArray("data");

您错过了添加 "daily" 对象。请试试这个。

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

        }

在您的 JSON 中,字符串 data 不是根节点,而是在 daily 节点内。所以首先你必须解析 daily 节点然后得到 data 数组。 尝试执行以下操作。

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

}

如果您正在努力使用 JSON,那么将 JSON 解析为 POJO 可能是值得的。虽然您可以正确使用 JSON,但这样做可能会更好,尤其是当您打算使用更多数据时。

首先创建您的 POJO 类。根据需要重命名它们。

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+"]";
    }
}

您可以将 Data[] 转换为 ArrayList,这样可以根据需要更轻松地使用它。

现在您想将 JSON 转换为 POJO。

为此,我们将使用 GSON,将其添加到您的 app.gradle 文件

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

现在要转换 JSON,编写如下代码:

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

现在您需要做的就是

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

您需要做的就是解决这个问题

getDaily()[0]

检查数据是否确实存在,否则可能会引发异常。