简体   繁体   中英

JSON.parse is not parsing complete JSON String

I had this problem in which json.parse is behaving weirdly. I am reading json from local storage and then saving it to the localstorage in cordova, but when I try to parse it and display its content in console it just shows only one element.

Below is my code.

console.log("about to display news: 
" +localStorage.getItem("getNewsLastUpdated"));

var cacheData = JSON.parse(JSON.stringify(eval("(" + 
localStorage.getItem(NewsCacheLocalStorage) + ")")));

console.log("CacheData: " + JSON.stringify(cacheData));

below is the screen shot of the same:

控制台中的输出

I am reading the file as show in the code below:

readCache = function (fs,filename,NewsCacheLocalStorage) {

fs.root.getFile(filename, {}, function(fileEntry) {

    fileEntry.file(function(file) {

        var reader = new FileReader();
        console.log("inside file read");
        reader.onloadend = function(e) {

            localStorage.setItem(NewsCacheLocalStorage, this.result);
            console.log("read onloadend: " + 
            localStorage.getItem(NewsCacheLocalStorage) );
        }

        reader.readAsText(file);

    }, errorReadCache);

}, errorReadCache);

}

Can any one tell me what is the problem here.

thank you in advance.

It's hard to tell because you've only presented the data in the form of a screenshot and not a [mcve], but it looks like your data consists of:

The comma operator:

evaluates each of its operands (from left to right) and returns the value of the last operand.

So it is only natural that you would end up with a single item.

If you want more than one item, put them in an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM