我正在使用jQuery Autocomplete,当我从自动完成结果中选择一条记录时,它会将其作为JSON返回:
{
"label": "123 Fakeville St",
"Address": "123 Fakeville St",
"YOC": 1994,
"value": "123 Fakeville St"
}
有没有办法可以重新格式化它,看起来像这样?
{
"house": {
"properties": {
"label": "123 Fakeville St",
"Address": "123 Fakeville St",
"YOC": 1994,
"value": "123 Fakeville St"
}
}
}
答案 0 :(得分:1)
只需将旧JSON放入属性house
下的新JSON对象中:
var oldObj = JSON.parse(oldJson);
var newObj = {
house: {
properties: oldObj
}
};
var newJson = JSON.stringify(newObj);
或者,在一行中,值应为:
JSON.stringify({house:{properties:JSON.parse(oldJson)}})
var oldJson = '{"label":"123 Fakeville St","Address":"123 Fakeville St","YOC":1994,"value":"123 Fakeville St"}';
console.log(JSON.stringify({house:{properties:JSON.parse(oldJson)}}));