使用jquery将json转换为字符串

时间:2022-05-22 21:49:46

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

我有一个嵌套的json。我想将其作为表单输入值发布。

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

但是,似乎jquery将“Object object”字符串放入值中。

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.

传递字符串并转换为我需要的本机形式似乎比处理json更容易,因为一旦生成它就不需要改变任何东西。

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()

1 个解决方案

#1


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.

jQuery没有JSON字符串化本机对象的方法。您将需要json2.js,它将为尚未支持它的浏览器提供JSON.stringify()方法。

#1


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.

jQuery没有JSON字符串化本机对象的方法。您将需要json2.js,它将为尚未支持它的浏览器提供JSON.stringify()方法。