最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert json to a string using jquery - Stack Overflow

programmeradmin2浏览0评论

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,
     "panyName": 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,
     "panyName": null
 }'

Following doesn't do what I need:

Json.stringify()

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,
     "panyName": 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,
     "panyName": null
 }'

Following doesn't do what I need:

Json.stringify()
Share Improve this question asked Mar 16, 2010 at 10:56 lprsdlprsd 87.1k48 gold badges140 silver badges169 bronze badges 2
  • Duplicate: stackoverflow./questions/191881/… – Ross Snyder Commented Mar 16, 2010 at 11:12
  • "Following doesn't do what I need:" -> How so? – Max Shawabkeh Commented Mar 16, 2010 at 11:14
Add a ment  | 

1 Answer 1

Reset to default 12

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.

发布评论

评论列表(0)

  1. 暂无评论