簡體   English   中英

JSON.parse未解析完整的JSON字符串

[英]JSON.parse is not parsing complete JSON String

我遇到了json.parse行為異常的問題。 我正在從本地存儲中讀取json,然后將其保存到cordova的localstorage中,但是當我嘗試解析它並將其內容顯示在控制台中時,它僅顯示一個元素。

下面是我的代碼。

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

以下是相同的屏幕截圖:

控制台中的輸出

我正在讀取文件,如下面的代碼所示:

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

}

誰能告訴我這里是什么問題。

先感謝您。

很難說,因為您僅以屏幕截圖而不是[mcve]的形式顯示了數據,但是看起來您的數據包括:

逗號運算符:

計算每個操作數(從左到右)並返回最后一個操作數的值。

因此,最終只得到一個項目是很自然的。

如果您需要多個項目,請將它們放在一個數組中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM