英文:
Android Studio Parsing JSON object doesn't work
问题
这是我的 JSON 文件。我试图将 JSON 加载到我的 RecyclerView 中,但在尝试从 body 对象中获取数组后,它会出现错误。
{
"body": {
"posters": [
{
"code": "3292d2r",
"attributes": [
"12-plus-trailer",
"2d",
"adventure",
"dubbed",
"dubbed-lang-hu",
"fantasy"
],
"featureTitle": "Artemis Fowl",
"weight": 0,
"dateStarted": "2020-05-28T00:00:00"
}
]
}
}
我的 Java 代码:
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONObject("body").getJSONArray("posters");
但是这种方法不起作用,有什么办法可以将 posters 中的项获取到一个 JSON 数组中吗?
JSON 文件结构:
{
"body": {
"posters": [
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
]
}
}
英文:
This is my json file. I am trying to load json in to my recyclerview, but after trying to get the array from the body object it runs on error.
{
body: {
posters: [
{
code: "3292d2r",
attributes: [
"12-plus-trailer",
"2d",
"adventure",
"dubbed",
"dubbed-lang-hu",
"fantasy"
],
featureTitle: "Artemis Fowl",
weight: 0,
dateStarted: "2020-05-28T00:00:00"
My java code:
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray=jsonObject.getJSONArray("body");
But this method doesn't work, any idea how I can get the items in posters to a JSONARRAY?
The JSON file structure:
{
body: {
posters: [
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
]
}
}
答案1
得分: 0
body
不是一个JSONArray,它是一个JSONObject。
jsonObject.getJSONObject("body").getJSONArray("posters");
英文:
body
is not a JSONArray. It is a JSONObject.
jsonObject.getJSONObject("body").getJSONArray("posters");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论