0

I have one array, one json object like this

["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"
    }
]

I just set this object as hidden variable like below

<input value="[object Object],[object Object],[object Object]" class="graphobj1" type="hidden">
<input value="www.flipkart.com","www.amazon.in" class="graphobj2" type="hidden">

When i retrive this json object back from the page like this

var z1 = $('.graphobj1');
var z2 = $('.graphobj2');

and when i alert this , it shows the error TypeError: cyclic object value, how can i get my objects return back as it is?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Juhan
  • 1,283
  • 2
  • 11
  • 30
  • How do you assign the object to the value? – satchcoder Feb 05 '15 at 07:41
  • http://stackoverflow.com/questions/9382167/serializing-object-that-contains-cyclic-object-value – BatScream Feb 05 '15 at 07:42
  • 1
    use JSON.stringify(object) to create the string you are asigning. Then jsut use JSON.parse() to get it back – satchcoder Feb 05 '15 at 07:42
  • I see no real reason for that error from the code you have shown. Is there anything else you are doing to the object? Also you can't store objects in values, you have to make them strings first. – Spokey Feb 05 '15 at 07:48

1 Answers1

0

Use JSON.stringify() and 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">
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188