在 Android 中解析 JSONArray
當遇到以 JSONArray 開頭的 JSON 資料時,了解解析過程至關重要。以下是如何處理提供的 JSON 來提取“名稱”值:
您已正確地將 JSONArray 標識為“abridged_cast”。但是,您的程式碼目前正在嘗試檢索“字元”資料。
要取得名稱,我們需要迭代 JSONArray 並從每個 JSON 物件中提取「名稱」欄位。下面是固定的程式碼片段:
try { //JSON is the JSON code provided in the question JSONObject jsonResponse = new JSONObject(JSON); JSONArray cast = jsonResponse.getJSONArray("abridged_cast"); List<string> allNames = new ArrayList(); for (int i = 0; i <p>程式碼迭代「abridged_cast」JSONArray,檢索每個 JSON 對象,並提取「name」字串。這些名稱儲存在 allNames 清單中,產生字串值:「Jeff Bridges,Charles Grodin,Jessica Lange,John Randolph,Rene Auberjonois.」</p></string>
以上是如何在 Android JSON 解析中從 JSONArray 中提取名稱?的詳細內容。更多資訊請關注PHP中文網其他相關文章!