gpt4 book ai didi

java - 在 Android/Java 中循环 JSONArray 时出现类型错误

转载 作者:行者123 更新时间:2023-12-01 12:16:05 40 4
gpt4 key购买 nike

我想访问海报并将其循环到这个 JSON 字符串中。

{"r":
{"posters":
[
{"rev":10,"poster_id":4,"poster_filename":"545397373c2c7","poster_filepath":"\/poster_files\/545397373c2c7","poster_synopsis":"This is the new synopsis for this exquisite poster I have created. You will marvel at its greatness and ask many questions.","poster_title":"Poster A"},
{"rev":6,"poster_id":7,"poster_filename":"5453b54b83b5f","poster_filepath":"\/poster_files\/5453b54b83b5f","poster_synopsis":"This is a synopsis that is not real. No one can say what this synopsis really means. It is total B.S..","poster_title":"Poster A"}
],
"msg":"72 & 2",
"status":"complete"}
}

我可以将字符串转换为 JSONObject:

JSONObject jsonObject = new JSONObject(result);         

JSONObject r = new JSONObject(jsonObject.getString("r"));

JSONArray posters = r.getJSONArray("posters");

Android Studio 提示“需要数组类型,当我尝试循环海报时发现 org.json.JSONArray:

 int arraySize = posters.length();

TextView myTextView = (TextView) findViewById(R.id.show_download_list);

for(int i = 0; i < arraySize; i++) {
myTextView.append(posters[i]);
myTextView.append("\n");
}

我没有正确转换对象吗?

最佳答案

JSONObject jsonObject = new JSONObject(result);         

JSONObject r = jsonObject.getJSONObject("r");

JSONArray posters = r.getJSONArray("posters");

我更改了 r JSONObject 以从名为 rJSONObject 获取它,而不是调用 string 称为 r ,,因为 json 字符串中的 r 是一个对象

更新

将循环中的第一行更改为 myTextView.append(posters.getJSONObject(i).getString("poster_title")); ,这将在每个 TextView 上打印海报标题。

  • 这里的问题是您将 JSON 数组作为普通数组来处理。

关于java - 在 Android/Java 中循环 JSONArray 时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27007034/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com