简体   繁体   English

json字符串解析错误

[英]json string parse error

I have a Json string as- 我有一个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'
}];

I want to parse it using jquery $.parseJSON to get each value. 我想用jQuery来分析它$.parseJSON获得每个值。 I have tried it using 我已经尝试过使用

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

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

But getting continuous error in each try. 但是每次尝试都会出现连续错误。 Can anyone tell a proper method for doing this. 任何人都可以告诉适当的方法来执行此操作。 Thanks in advance. 提前致谢。

You're trying to turn a JavaScript object into a JavaScript object, which just doesn't make sense! 您试图将一个JavaScript对象变成一个JavaScript对象,但这根本没有意义! What you can do is 你能做的是

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

and use str to transfer your data somewhere else. 并使用str将数据传输到其他地方。 Then use 然后使用

var obj = JSON.parse(str);

to get your original object back after it's been modified (or not) from another source. 从其他来源修改(或不修改)原始对象后,将其取回。

You use parseJSON to parse a jsonString to json object. 您可以使用parseJSON将jsonString解析为json对象。

But in your case RetailerData.webSites is already a json object no need to parse it. 但是在您的情况下, 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