- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道那里有大量的 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) 收集此信息并将“摘要”值放入另一个 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,那么将 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]
检查数据是否确实存在,否则会抛出异常。
关于android - 在 Android Studio 中从 JSON 获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59247443/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!