2
{
    "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
        }
    }
}

I would like to get "pages"=>"files", but return org.json.JSONException: No value for files

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);
}
4
  • thanks for attention, it seems some syntax error only Commented Dec 12, 2013 at 4:14
  • JSONArray files = page.getJSONArray("files"); here, use page instead of pages. Commented Dec 12, 2013 at 4:16
  • probably some objects not contains files JSONArray so either use optJSONArray to get JSONArray which return null if key not found or use JSONObject.hasJSONObject.isNull` before accessing values from files JSONArray Commented Dec 12, 2013 at 4:17
  • 1
    Typo in your code. Beware of the variables' name. Commented Dec 12, 2013 at 4:22

3 Answers 3

3

Instead of getting files JSONArray from page JSONObject you are trying to get it from pages JSONObject.. Try this:

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Mistake:

You are trying to get files from pages object instead of page object.

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

Solution:

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

Comments

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

Since files is not the direct child of the root `JSONObject use the below method.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.