使用JSON.stringify()将JSON对象转换为String时出现循环对象值错误

时间:2015-05-26 03:43:31

标签: javascript jquery json

我有一个格式为的JSON对象:

obj { 
    name: "abc" , 
    entriesList : "list of entry obj" , 
    propertiesList : "list of properties obj" 
};

其中,条目也是另一个对象

entry { 
    info : "data obj" , 
    age : "15" , 
    subjects : "5"
}
properties { 
    a : "a" ,  
    b : "b" 
}
data { 
    c : "c" , 
    d : "d"
}

使用JSON.stringify()会出错

  

循环对象值

我应该如何将对象转换为JSON字符串?

1 个答案:

答案 0 :(得分:0)

我无法从您的示例中看到一个循环,但我们的想法是不在您的对象中包含循环引用。我的意思是避免这样的事情:

var a = {}, b = {};
a.child = b;
b.child = a; //This will cause a cyclic reference when calling JSON.stringify both on a and b object