簡體   English   中英

在Android / Java中循環JSONArray時出現類型錯誤

[英]I am getting a type error when looping JSONArray in Android / Java

我想訪問海報並在此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 的JSONObjectJSONObject的簡稱R,而不是調用簡稱R得到它,,因為的R JSON字符串是一個對象

更新

將循環中的第一行更改為myTextView.append(posters.getJSONObject(i).getString("poster_title")); ,這將在每個文本視圖上打印海報標題。

  • 這里的問題是您將JSON數組作為普通數組處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM