我有这个 Javascript 对象(由我的插件代码即时创建):
{
"field": {
"name": "Name",
"surname": "Surname"
},
"address": {
"street": "Street",
"number": 0,
"postcode": 0,
"geo": {
"city": "City",
"country": "Country",
"state": "State"
}
},
"options": [1,4,6,8,11]
}
我不想将此对象转换为 JSON 字符串,但我想将此对象转换为另一个对象,但每个字段都由字符串表示,如下所示:
{
"field[name]": "Name",
"field[surname]": "Surname",
"address[street]": "Street",
"address[number]": 0,
"address[postcode]": 0,
"address[geo][city]": "City",
"address[geo][country]": "Country",
"address[geo][state]": "State",
"options[0]":1,
"options[1]":4,
"options[2]":6,
"options[3]":8,
"options[4]":11
}
设想:
- 我不知道原始对象会是什么样子(或者它会有多深),因为它是插件的一部分,我不知道人们将如何构建他们的表单
- 我要把这个新对象放在一个
FormData
对象里面,如果它只接受对象,那就更容易了,因为JSON不能上传文件,但FormData
对象可以