2013-12-12 97 views
1
{ 
    "issueName": "2013-11-12", 
    "direction": "rtl", 
    "timestamp": "1384184763", 
    "download": { 
     "preview": "preview.zip", 
     "content": { 
      "count": 9, 
      "files": ["1.zip", 
      "2.zip", 
      "3.zip", 
      "4.zip", 
      "5.zip", 
      "6.zip", 
      "7.zip", 
      "8.zip", 
      "9.zip"] 
     }, 
     "thumbnail": "thumbnail.zip", 
     "advertorial": "advertorial.zip" 
    }, 
    "pages": { 
     "1": { 
      "pageNo": "1", 
      "files": ["Pg001.png", 
      "Web201311_P1_medium.jpg", 
      "Pg001_142_p.jpg", 
      "Pg001_142_t.png", 
      "Pg001_142_p_ios.jpg", 
      "Pg001_142_t_ios.png"], 
      "section": 0 
     }, 
     "2": { 
      "pageNo": "2", 
      **"files"**: ["Pg002.png", 
      "Web201311_P2_medium.jpg", 
      "Pg002_142_p.jpg", 
      "Pg002_142_t.png", 
      "Pg002_142_p_ios.jpg", 
      "Pg002_142_t_ios.png"], 
      "section": 0 
     } 
    } 
} 

我想獲得「頁面」=> 「文件」,但返回org.json.JSONException: No value for files獲取從JSON數組中的Android

JSONObject issueInfo = new JSONObject(filePath)); 
JSONObject pages = issueInfo.getJSONObject("pages"); 
Iterator iterator = pages.keys(); 

while (iterator.hasNext()) { 
    String key = (String) iterator.next(); 
    JSONObject page = pages.getJSONObject(key); 
    JSONArray files = pages.getJSONArray("files"); 
    String _thumbUri = files.getString(1); 
    String _graphicUri = files.getString(2); 
    String _textUri = files.getString(3); 
} 
+0

感謝注意,它似乎是索姆e語法錯誤 – user782104

+0

'JSONArray files = page.getJSONArray(「files」);'在這裏,使用'page'而不是'pages'。 – Jhanvi

+0

可能有些對象不包含'files' JSONArray,所以要麼使用'optJSONArray'來獲取JSONArray,如果沒有找到鍵或者在從'files'訪問值之前使用'JSONObject.has'JSONObject.isNull'返回'null' JSONArray –

回答

2

而是從JSONObject你正試圖從JSONObject頁面得到它越來越files JSONArray的..試試這個:

JSONObject issueInfo = new JSONObject(filePath)); 
JSONObject pages = issueInfo.getJSONObject("pages"); 
Iterator iterator = pages.keys(); 

while (iterator.hasNext()) { 
    String key = (String) iterator.next(); 
    JSONObject page = pages.getJSONObject(key); 
    JSONArray files = page.getJSONArray("files"); //Correction here 
    String _thumbUri = files.getString(1); 
    String _graphicUri = files.getString(2); 
    String _textUri = files.getString(3); 
} 
1
JSONArray files = pages.getJSONArray("files"); 

由於文件是不是該直接子根`JSONObject使用下面的方法。

JSONObject page = pages.getJSONObject(key).getJSONArray("files"); 
2

錯誤:

您正試圖獲得filespages對象,而不是page對象。

JSONArray files = pages.getJSONArray("files"); 

解決方案:

JSONArray files = page.getJSONArray("files");