【问题标题】:Modify JSON string with javascript [duplicate]用javascript修改JSON字符串[重复]
【发布时间】:2016-12-01 21:15:56
【问题描述】:

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

有一个用例,如果值为 true,我需要向这些设置添加一个选项。

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

我尝试将 JSON 存储为一个数组,然后将我的新数据推送到其中,但 java-script 抱怨它的格式不正确。

如何将额外的 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"
  }

}

【问题讨论】:

  • 您没有 JSON。 JSON 是一种可以解析为数据的文本格式。你有一个对象。因此,您可以像任何其他对象一样为其添加属性。

标签: javascript json


【解决方案1】:

你展示的只是一个普通的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多