admin管理员组

文章数量:1410737

I wanted to converted the javascript object into string so that it should work in all the browsers

I used object.toSource(); to convert it to string. It work's fine only in firefox.

In IE it shows

Object doesn't support this property or method

I replaced it with JSON.stringify(object); this time i get an error as "Circular reference in value argument not supported". I'm not sure what the problem is.

I got this output when I use object.toSource();

The jJSON data is

({test:["456", "Test event", (new Date(1332131400000)), (new Date(1332135000000)), "0", 0, 0, "16", 1, "Some place", "...............

I need to convert this JSON data into string.......

Can any one help me on this?
Thanks

I wanted to converted the javascript object into string so that it should work in all the browsers

I used object.toSource(); to convert it to string. It work's fine only in firefox.

In IE it shows

Object doesn't support this property or method

I replaced it with JSON.stringify(object); this time i get an error as "Circular reference in value argument not supported". I'm not sure what the problem is.

I got this output when I use object.toSource();

The jJSON data is

({test:["456", "Test event", (new Date(1332131400000)), (new Date(1332135000000)), "0", 0, 0, "16", 1, "Some place", "...............

I need to convert this JSON data into string.......

Can any one help me on this?
Thanks

Share Improve this question edited Apr 29, 2015 at 9:50 Anik Islam Abhi 25.4k8 gold badges59 silver badges81 bronze badges asked Mar 19, 2012 at 9:59 saransaran 5955 gold badges17 silver badges28 bronze badges 4
  • I have no answer to your question, but I wanna know, when do you ever want to convert your object to a string? – Amberlamps Commented Mar 19, 2012 at 10:03
  • How do you want the object to be represented in the string? – Guffa Commented Mar 19, 2012 at 10:05
  • object.toSource() gives "({test:["456", "Test event", (new Date(1332131400000)), (new Date(1332135000000)), "0", 0, 0, "16", 1, "Some place", "..............." The same is not working in IE. This is my main problem.. – saran Commented Mar 19, 2012 at 10:35
  • 1 Probably worth noting that JSON stands for "JavaScript Object Notation" and is based on javascript object literal syntax. So it should e as no surprise that it can't represent structures or relationships that can't be represented by a javascript object literal. – RobG Commented Mar 20, 2012 at 13:40
Add a ment  | 

3 Answers 3

Reset to default 6

The toSource() method is not supported in IE; JavaScript implementations of browsers are known to be somewhat different, this is one of those cases.

JSON can't represent circular references. This is an example of a circular reference:

var a = {}, b = {a: a};
a.b = b;
// Now I can go a.b.a.b.a.b.a.b... forever; there's no way to represent this in JSON

You should probably implement your own serialization method, possibly by overriding toString(). If you want to stick with JSON, you'll have to remove the circular reference.

Circular reference means just that; there is a circular reference in your object. For instance, imagine the js code:

var a = {a: 'test'};
a.b = a;

Now we want to stringify a. We start with {"a":"test","b":, then we see that a.b is an object, ok, we call stringify on that too and end up with {"a":"test","b":{"a":"test","b": and so forth. As you can probably see, this cycle will never end, thus you have a circular reference which can't be serialized in this manner.

May be you will interested in JSONplus.It can solve "Circular reference".But its document is chinese.

本文标签: jsonHow to convert javascript object into stringStack Overflow