views:

77

answers:

1

I have a nested json. I want to post it as a form input value.

But, seems like jquery puts "Object object" string into the value.

It seems easier to pass around the string and convert into the native form I need, than dealing with json as I don't need to change anything once it is generated.

What is the simplest way to convert a json

var json = {
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
     "streetAddress": "21 2nd Street",
     "city": "New York",
     "state": "NY",
     "postalCode": "10021"
     },
     "phoneNumber": [
     { "type": "home", "number": "212 555-1234" },
     { "type": "fax", "number": "646 555-4567" }
     ],
     "newSubscription": false,
     "companyName": null
 };

into its string form?

var json = '{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
     "streetAddress": "21 2nd Street",
     "city": "New York",
     "state": "NY",
     "postalCode": "10021"
     },
     "phoneNumber": [
     { "type": "home", "number": "212 555-1234" },
     { "type": "fax", "number": "646 555-4567" }
     ],
     "newSubscription": false,
     "companyName": null
 }'

Following doesn't do what I need:

Json.stringify()
+3  A: 

AFAIK, jQuery doesn't have a method for JSON stringifying native objects. You will need json2.js which will provide the JSON.Stringify() method to browsers that don't already support it.

Andy E