如何重新创建json对象

时间:2015-07-07 09:24:51

标签: javascript json angularjs

我有像这样的json对象

[
      {
        "name": "first_name",
        "value": "sssssssssssssssssss"
      },{
        "name": "email",
        "value": "ss.ss@gmail.com"
      }, {
        "name": "address",
        "value": "ssssssssssssssssssss"
      }, {
        "name": "PhoneNumber",
        "value": "12342123321"
      }     
    ]

这些数据正在以分包为单位

但我希望json数据为

{
    "formProperties": {
        "table": "users",
        "mode": "insert",
        "method":"post",
        "action":"urlhere",
        "user":"admin"
  },
    "formValues": [
      {
        "name": "first_name",
        "value": "sssssssssssssssssss"
      },{
        "name": "email",
        "value": "ss.ss@gmail.com"
      }, {
        "name": "address",
        "value": "ssssssssssssssssssss"
      }, {
        "name": "PhoneNumber",
        "value": "12342123321"
      }     
    ]
}

如何重建JSON对象。请帮助我的朋友我遇到了这个问题。

提前致谢

1 个答案:

答案 0 :(得分:2)

假设JSON为字符串保存在名为json的变量中,您可以使用以下代码:

var newObject = {
    "formProperties": {
        "table": "users",
        "mode": "insert",
        "method":"post",
        "action":"urlhere",
        "user":"admin"
    },
    "formValues": JSON.parse(json)
};

var newJson = JSON.stringify(newObject);