簡體   English   中英

json字符串解析錯誤

[英]json string parse error

我有一個Json字符串-

var RetailerData = {};

// The object that the JSON string should represent, can use this as it is if you want.

RetailerData.webSites = [
{
    id: 1,
    text: 'J.Crew',
    image: 'images/retailer-logo/jcrew.png',
    extra: 'www.jcrew.com'
},
{
    id: 2,
    text: 'GAP',
    image: 'images/retailer-logo/gap.png',
    extra: 'www.gap.com'
}];

我想用jQuery來分析它$.parseJSON獲得每個值。 我已經嘗試過使用

var obj = $.parseJSON(RetailerData.webSites);

$.each(obj, function() {
    console.log(this['id']);
});

但是每次嘗試都會出現連續錯誤。 任何人都可以告訴適當的方法來執行此操作。 提前致謝。

您試圖將一個JavaScript對象變成一個JavaScript對象,但這根本沒有意義! 你能做的是

var str = JSON.stringify(RetailerData.webSites);

並使用str將數據傳輸到其他地方。 然后使用

var obj = JSON.parse(str);

從其他來源修改(或不修改)原始對象后,將其取回。

您可以使用parseJSON將jsonString解析為json對象。

但是在您的情況下, RetailerData.webSites已經是一個json對象,無需解析它。

var obj = RetailerData.webSites;

$.each(obj, function() {
    console.log(this['id']);
});

暫無
暫無

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

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