有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Android/Java中循环JSONArray时,我遇到了一个类型错误

我想访问海报并在这个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");
 }

我没有正确地转换对象吗


共 (1) 个答案

  1. # 1 楼答案

    JSONObject jsonObject = new JSONObject(result);         
    
    JSONObject r = jsonObject.getJSONObject("r");
    
    JSONArray posters = r.getJSONArray("posters");
    

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

    更新

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

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