我想使用数组列出 JSON 字符串中的数据,但为啥我的应用程序崩溃了?
Posted
技术标签:
【中文标题】我想使用数组列出 JSON 字符串中的数据,但为啥我的应用程序崩溃了?【英文标题】:I want to list data from JSON string using an array, but why my app crashes?我想使用数组列出 JSON 字符串中的数据,但为什么我的应用程序崩溃了? 【发布时间】:2020-03-08 15:35:20 【问题描述】:我想在 ListView 中列出来自 JSON 字符串的数据。
我将字符串存储在一个数组中。
当我在JSONObject day = jsonArray.getJSONObject(0);
行中写i
时,应用程序崩溃了。
我做错了什么?
String json_string1 = "\"list\":[\"dt\":1471255200,\"temp\":\"day\":30.04,\"min\":17.35,\"max\":30.04,\"pressure\":1018.83,\"humidity\":35,\"weather\":[\"id\":802,\"main\":\"Clouds\",\"de scription\":\"scattered clouds\",\"icon\":\"03d\"],\"speed\":2.66,\"deg\":314,\"clouds\":48,\"dt\":1471341600,\"temp\":\"day\":29.48,\"min\":19.3,\"max\":29.95,\"pressure\":1018.63,\"humidity\":100,\"weather\":[\"id\":501,\"main\":\"Rain\",\"des cription\":\"moderate rain\",\"icon\":\"10d\"],\"speed\":2.46,\"deg\":28,\"clouds\":92,\"rain\":8.5,\"dt\":1471428000,\"temp\":\"day\":28.96,\"min\":17.84,\"m ax\":297.54,\"pressure\":1012.1,\"humidity\":95,\"weather\":[\"id\":501,\"main\":\"Ra in\",\"description\":\"moderate rain\",\"icon\":\"10d\"],\"speed\":2.27,\"deg\":118,\"clouds\":88,\"rain\":5.55,\"dt\":1471514400,\"temp\":\"day\":26.89,\"min\":17.84,\" max\":28.95,\"pressure\":1014.27,\"humidity\":83,\"weather\":[\"id\":500,\"main\":\"R ain\",\"description\":\"light rain\",\"icon\":\"10d\"],\"speed\":1.98,\"deg\":93,\"clouds\":20,\"dt\":1471600800,\"temp\":\"day\":30.04,\"min\":21.87,\"max\":30.04,\"pressure\":1014.65,\"humidity\":0,\"weather\":[\"id\":500,\"main\":\"Rain\",\"descrip tion\":\"light rain\",\"icon\":\"10d\"],\"speed\":1.66,\"deg\":194,\"clouds\":11,\"rain\":0.22,\"dt\":1471687200,\"temp\":\"day\":31.3,\"min\":22.56,\"max\":32.3,\"p ressure\":1018.6,\"humidity\":0,\"weather\":[\"id\":500,\"main\":\"Rain\",\"descriptio n\":\"light rain\",\"icon\":\"10d\"],\"speed\":1.69,\"deg\":180,\"clouds\":16,\"rain\":0.77,\"dt\":1471773600,\"temp\":\"day\":31.77,\"min\":21.67,\"max\":30.77,\"pressure\":1016.93,\"humidity\":0,\"weather\":[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"],\"speed\":2.49,\"deg\":227,\"clouds\":14,\"rain\":2.2]";
JSONObject jsonObject;
String[] days_array = new String[7];
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
jsonObject = new JSONObject(json_string1);
JSONArray jsonArray = jsonObject.optJSONArray("list");
int lengthJsonArr = jsonArray.length();
for (int i = 0; i < lengthJsonArr; i++)
JSONObject day = jsonArray.getJSONObject(0);
JSONObject temp = day.getJSONObject("temp");
int min = temp.getInt("min");
int max = temp.getInt("max");
JSONArray weather = day.getJSONArray("weather");
JSONObject zero = weather.getJSONObject(0);
String main = zero.getString("main");
double speed = day.getDouble("speed");
days_array[i] = max + "/" + min + ", " + main + ", Wind=" + speed;
catch (JSONException e)
e.printStackTrace();
ListView ListView = (ListView) findViewById(R.id.list_view);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.item_name, days_array);
ListView.setAdapter(adapter);
【问题讨论】:
【参考方案1】:您有两个错误的属性名称:"m ax"
和 " max"
。这个固定的 json 可以工作:
String json_string1 = "\"list\":[\"dt\":1471255200,\"temp\":\"day\":30.04,\"min\":17.35,\"max\":30.04,\"pressure\":1018.83,\"humidity\":35,\"weather\":[\"id\":802,\"main\":\"Clouds\",\"de scription\":\"scattered clouds\",\"icon\":\"03d\"],\"speed\":2.66,\"deg\":314,\"clouds\":48,\"dt\":1471341600,\"temp\":\"day\":29.48,\"min\":19.3,\"max\":29.95,\"pressure\":1018.63,\"humidity\":100,\"weather\":[\"id\":501,\"main\":\"Rain\",\"des cription\":\"moderate rain\",\"icon\":\"10d\"],\"speed\":2.46,\"deg\":28,\"clouds\":92,\"rain\":8.5,\"dt\":1471428000,\"temp\":\"day\":28.96,\"min\":17.84,\"max\":297.54,\"pressure\":1012.1,\"humidity\":95,\"weather\":[\"id\":501,\"main\":\"Ra in\",\"description\":\"moderate rain\",\"icon\":\"10d\"],\"speed\":2.27,\"deg\":118,\"clouds\":88,\"rain\":5.55,\"dt\":1471514400,\"temp\":\"day\":26.89,\"min\":17.84,\"max\":28.95,\"pressure\":1014.27,\"humidity\":83,\"weather\":[\"id\":500,\"main\":\"R ain\",\"description\":\"light rain\",\"icon\":\"10d\"],\"speed\":1.98,\"deg\":93,\"clouds\":20,\"dt\":1471600800,\"temp\":\"day\":30.04,\"min\":21.87,\"max\":30.04,\"pressure\":1014.65,\"humidity\":0,\"weather\":[\"id\":500,\"main\":\"Rain\",\"descrip tion\":\"light rain\",\"icon\":\"10d\"],\"speed\":1.66,\"deg\":194,\"clouds\":11,\"rain\":0.22,\"dt\":1471687200,\"temp\":\"day\":31.3,\"min\":22.56,\"max\":32.3,\"p ressure\":1018.6,\"humidity\":0,\"weather\":[\"id\":500,\"main\":\"Rain\",\"descriptio n\":\"light rain\",\"icon\":\"10d\"],\"speed\":1.69,\"deg\":180,\"clouds\":16,\"rain\":0.77,\"dt\":1471773600,\"temp\":\"day\":31.77,\"min\":21.67,\"max\":30.77,\"pressure\":1016.93,\"humidity\":0,\"weather\":[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"],\"speed\":2.49,\"deg\":227,\"clouds\":14,\"rain\":2.2]";
为确保不再发生这种情况,您可以使用在线工具(例如下一个https://jsoneditoronline.org/)来验证您未来的 json,然后再认为您的代码有任何错误。
还有一件事,您还可以通过String jsonString1
改进变量String json_string1
的名称以保持java 的驼峰式大小写约定
【讨论】:
以上是关于我想使用数组列出 JSON 字符串中的数据,但为啥我的应用程序崩溃了?的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Javascript 不能从字符串文字中解析这个 JSON 数组?
为啥使用 copy() 时字符串和数组的处理方式不同? [复制]
为啥 json_extract 有效,但 json_extract_scalar 无效?
如何在flutter中使用mysql数据库(或json)中的sharedpreferences键选择以列出最喜欢的记录?