使用JavaScript附加到JSON对象

时间:2010-10-10 01:05:41

标签: javascript json

我正在使用JavaScript构建JSON对象。如何将以下数据插入堆栈底部:

"hello": { "label":"Hello", "url":"#hello" }

进入以下变量:

var ListData = {
  "main": {
    "label":"Main",
    "url":"#main"
  },
  "project": {
    "label":"Project",
    "url":"#project"
  },
  "settings": {
    "label":"Settings",
    "url":"#settings",
    "subnav":[
      {
        "label":"Privacy",
        "url":"#privacy"
      },
      {
        "label":"Security",
        "url":"#security"
      },
      {
        "label":"Advanced",
        "url":"#advanced"
      }
    ]
  }
};

所以变量看起来像:

var ListData = {
  "main": {
    "label":"Main",
    "url":"#main"
  },
  "project": {
    "label":"Project",
    "url":"#project"
  },
  "settings": {
    "label":"Settings",
    "url":"#settings",
    "subnav":[
      {
        "label":"Privacy",
        "url":"#privacy"
      },
      {
        "label":"Security",
        "url":"#security"
      },
      {
        "label":"Advanced",
        "url":"#advanced"
      }
    ]
  },
  "hello": {
    "label":"Hello",
    "url":"#hello"
  }
};

我使用了以下代码,但它似乎不起作用:

var NewData = '"hello": { "label":"Hello", "url":"#hello" }';
ListData.push(NewData);

5 个答案:

答案 0 :(得分:15)

您可以直接使用对象文字插入它:

ListData.hello = { label: "Hello", url: "#hello" }; 

答案 1 :(得分:5)

如果您使用的是jQuery,则可以使用。extend() jQuery API,如:

$.extend(ListData, {"hello": { "label":"Hello", "url":"#hello" }});

答案 2 :(得分:2)

我还有一个使用underscore.js模块的解决方案,

var _ = require("underscore");

var src = {
    "main": {
        "label": "Main",
        "url": "#main"
    },
    "project": {
        "label": "Project",
        "url": "#project"
    },
    "settings": {
        "label": "Settings",
        "url": "#settings",
        "subnav": [
            {
                "label": "Privacy",
                "url": "#privacy"
            },
            {
                "label": "Security",
                "url": "#security"
            },
            {
                "label": "Advanced",
                "url": "#advanced"
            }
        ]
    }
};

var dest = {"hello": { "label":"Hello", "url":"#hello" }};


var data = _.extend(src, dest);
console.log(JSON.stringify(data));

必需的操作:

{"main":{"label":"Main","url":"#main"},"project":{"label":"Project","url":"#project"},"settings":{"label":"Settings","url":"#settings","subnav":[{"label":"Privacy","url":"#privacy"},{"label":"Security","url":"#security"},{"label":"Advanced","url":"#advanced"}]},"hello":{"label":"Hello","url":"#hello"}}

答案 3 :(得分:1)

JavaScript Object Literal是由一对花括号包装的以逗号分隔的名称/值对列表。

要将 encampment 名称的属性名称附加值 Valley Forge 堆栈底部,只需添加属性名称在带有点语法的JSON对象之后。然后指定值。 (参见'追加数据'下面)

您还可以从对象文字中删除附加的名称/值对。 (请参阅'删除以下数据')

// Start with some JSON
var myJson = { "name":"George Washington", "rank":"General", "serial":"102" };

// Append data
myJson.encampment = "Valley Forge";    

// Delete data
delete myJson.encampment

答案 4 :(得分:0)

保持对象文字语句只需将另一个对象添加到ListData对象。

ListData.hello = { "label":"Hello", "url":"#hello" };

push仅适用于Javascript Arrays。