使用javascript修改JSON字符串

时间:2016-12-01 21:15:56

标签: javascript json

我有一个JSON字符串,硬编码到我的页面中,它用于我的应用程序中的设置。

如果值为true,我需要在这些设置中添加一个选项。

从我下面的伪代码中,我正在尝试添加"设置"如果值为true,则为主字符串。

我尝试将JSON存储为数组,然后将新数据推入其中,但java脚本抱怨它没有采用正确的格式。

如何将其他json数据添加到我的主字符串?

    var v = true, 
    test = {
    "copySelected": {
        "name": "Copy",
        "icon": "fa-files-o"
    },
    "sep1": "---------",
    "success": {
        "name": "Highlight: Green",
        "icon": "fa-pencil"
    },
    "info": {
        "name": "Highlight: Blue",
        "icon": "fa-pencil"
    },
    "warning": {
        "name": "Highlight: Yellow",
        "icon": "fa-pencil"
    },
    "danger": {
        "name": "Highlight: Red",
        "icon": "fa-pencil"
    },
    "sep2": "---------",
    "remove": {
        "name": "Remove Highlight",
        "icon": "fa-eraser"
    },
    "sep3": "---------",
    "addNote": {
        "name": "Add Note",
        "icon": "fa-file-text-o"
    }
}

// I need to add this section to the above json at the end
if(v){

  "sep4": "---------",
  "removeUser": {
      "name": "Remove User",
      "icon": "fa-user-times"
  }

}

1 个答案:

答案 0 :(得分:3)

您展示的只是一个常规JavaScript对象。只需正常添加属性:



    var v = true, 
    test = {
    "copySelected": {
        "name": "Copy",
        "icon": "fa-files-o"
    },
    "sep1": "---------",
    "success": {
        "name": "Highlight: Green",
        "icon": "fa-pencil"
    },
    "info": {
        "name": "Highlight: Blue",
        "icon": "fa-pencil"
    },
    "warning": {
        "name": "Highlight: Yellow",
        "icon": "fa-pencil"
    },
    "danger": {
        "name": "Highlight: Red",
        "icon": "fa-pencil"
    },
    "sep2": "---------",
    "remove": {
        "name": "Remove Highlight",
        "icon": "fa-eraser"
    },
    "sep3": "---------",
    "addNote": {
        "name": "Add Note",
        "icon": "fa-file-text-o"
    }
}

// I need to add this section to the above json at the end
if(v){

  test.sep4 =  "---------";
  test.removeUser = {
      name: "Remove User",
      icon: "fa-user-times"
  };

}

console.log(test.sep4)
console.log(test.removeUser);




如果您收到了JSON字符串,那么您只需调用JSON.parse(string),返回值将是一个对象,您只需添加属性即可,如此处所示。 (注意:对象现在封装在引号中,只是一个字符串。)



        var v = true, 
        test = `{
        "copySelected": {
            "name": "Copy",
            "icon": "fa-files-o"
        },
        "sep1": "---------",
        "success": {
            "name": "Highlight: Green",
            "icon": "fa-pencil"
        },
        "info": {
            "name": "Highlight: Blue",
            "icon": "fa-pencil"
        },
        "warning": {
            "name": "Highlight: Yellow",
            "icon": "fa-pencil"
        },
        "danger": {
            "name": "Highlight: Red",
            "icon": "fa-pencil"
        },
        "sep2": "---------",
        "remove": {
            "name": "Remove Highlight",
            "icon": "fa-eraser"
        },
        "sep3": "---------",
        "addNote": {
            "name": "Add Note",
            "icon": "fa-file-text-o"
        }
    }`
    
    var result = JSON.parse(test);

    // I need to add this section to the above json at the end
    if(v){

      result.sep4 =  "---------";
      result.removeUser = {
          name: "Remove User",
          icon: "fa-user-times"
      };

    }

    console.log(result.sep4)
    console.log(result.removeUser);