" JSON错误"用readAsText读取json文件

时间:2014-10-03 23:30:33

标签: json fileapi intel-xdk

我正在读取包含一些json代码的文件,我想从中创建一个对象。 读取此文件会出现错误" JSON错误" (参见下面的事件数据)。而且我不明白为什么。

我的阅读代码:

PageClass.createFromFile = function (dirEntry,pages) {
    "use strict";

    console.log("trying to create page for folder " + dirEntry.name);
    dirEntry.getFile("page.properties", {create: false}, function (fileEntry) {
        var reader = new FileReader();
        reader.onload = function (evt) {
            console.log("Read as text done for "+fileEntry.fullPath);
            console.log(evt);
            var json = evt.target.result;
            console.log(json);
            var page = PageClass.createFromJSON(json);
            page.root = dirEntry.name;
            console.log("Read page is "+page.number);
            pages.push(page);
        };
        reader.onerror = function (evt) {
            console.log("Read as text **failed** for "+fileEntry.fullPath);
            console.log(evt);
        };
        reader.readAsText(fileEntry);
    }, function (evt) {
        console.warn("page.properties non trouvé pour " + dirEntry.fullPath);
        if (evt.code !== FileError.NOT_FOUND_ERR) {
            fail(evt);
        }
    }
    );

};

文件内容:  {"preferred_image":"","source_image":"","title":"","bookmark":"","number":"002-003","commons":"","status":"0"}

来自onerror的progressEvent:

ProgressEvent
    bubbles: false
    cancelBubble: false
    cancelable: false
    lengthComputable: false
    loaded: 0
    target: Object
        _error: FileError
            code: "JSON error"
            __proto__: —
        _fileName: "file:///storage/emulated/0/Android/data/com.intel.html5tools.apppreview/cache/00001/page.properties"
        _readyState: 2
        _realReader: FileReader
        _result: null
        __proto__: —
    total: 0
    type: "error"
    __proto__

有什么想法吗?

1 个答案:

答案 0 :(得分:0)