我有一个数组,一个像这样的json对象
["www.flipkart.com","www.amazon.in"]
[
{
"product_name": "Deepcool",
"dollar_price": 2.9697123914957047,
"product_code": "C60_4123"
},
{
"product_name": "Deepcool XFAN",
"dollar_price": 5.473902300000191,
"product_code": "C60_4123"
},
{
"product_name": "DeepcoolXFAN80",
"dollar_price": 5.473902300000191,
"product_code": "C60_4123"
}
]
我只是将此对象设置为隐藏变量,如下所示
<input value="[object Object],[object Object],[object Object]" class="graphobj1" type="hidden">
<input value="www.flipkart.com","www.amazon.in" class="graphobj2" type="hidden">
当我从页面中回溯这个json对象时,就像这样
var z1 = $('.graphobj1');
var z2 = $('.graphobj2');
当我提醒它时,它显示错误TypeError: cyclic object value
,我怎样才能让我的对象返回原样?
答案 0 :(得分:0)
使用 JSON.stringify()
和 JSON.parse()
var a = ["www.flipkart.com", "www.amazon.in"],
b = [{
"product_name": "Deepcool",
"dollar_price": 2.9697123914957047,
"product_code": "C60_4123"
}, {
"product_name": "Deepcool XFAN",
"dollar_price": 5.473902300000191,
"product_code": "C60_4123"
}, {
"product_name": "DeepcoolXFAN80",
"dollar_price": 5.473902300000191,
"product_code": "C60_4123"
}],
$input1 = $('input.graphobj1'),
$input2 = $('input.graphobj2');
$input1.val(JSON.stringify(a));
$input2.val(JSON.stringify(b));
console.log(JSON.parse($input1.val()));
console.log(JSON.parse($input2.val()));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input class="graphobj1" type="hidden">
<input class="graphobj2" type="hidden">