java - 从android中的json获取数组

标签 java android json

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

我想获取"pages"=>"files",但返回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);
}

最佳答案

不是从 page JSONObject 获取 files JSONArray 你试图从 pages JSONObject..试试这个:

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);
}

关于java - 从android中的json获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20534880/

相关文章:

javascript - 如果键是数字,它会自动与从 JSON 创建的表中的左侧对齐

javascript - 将 json 数据转换为 javascript 数组

javascript - 将Json数据解析成函数

java - 调用 SwingWorker 方法 get() 时 gui 卡住

java - 使用SAX解析器,需要所有xml元素(在特定元素下)作为字符串

java - 在 Java8 中模拟 Lazy

android - 设置 onitemclicklistener 以 listview 显示搜索 Activity 搜索结果

android - 如何将 Gesture Overlay 下的 View 方向设置为 "none"

java - 为什么 JDK 中会有 Void 包装类?

java - 将空格和特殊字符传递给 RestFul Web 服务