2

我正在尝试制作一个 JSON 来存储有关用户的信息。例如,最后它希望它看起来像:

{
"people": [
    {
        "name": "foo",
        "company": "bar",
        "email": "foo@bar.com"
    },
    {
        "name": "bar",
        "company": "foo",
        "email": "bar@foo.com"
    }
]
}

这将通过一个表单填充,因此我希望能够声明一个空 JSON 并在人们提交表单时添加到它。有没有办法用骨架(姓名、公司、电子邮件)声明一个空的 JSON?另外我如何添加另一个条目?是否有某种 .append("name": "bar", "company": "foo", "email": "bar@foo.com") ?我想直接使用 javascript,但如果需要,使用 jquery 就可以了。

4

4 回答 4

2

使用 Javascript,无需在 JSON 对象中预定义任何内容。

这是完全有效的代码:

var o = {}; // create a new object

o.name = "Bob";
console.log(o.name); // prints Bob

o.scores = new Array();
o.scores[o.scores.length] = 1;
o.scores[o.scores.length] = 2;
o.scores[o.scores.length] = 3;
console.log(o.scores); // prints [1, 2, 3]

o.info = {};

o.info.isSet = true;
console.log(o.info.isSet); // prints true

console.log(o.info.isNotSet); // prints undefined

因此,您可以从表单中动态构建所需的任何类型的 JSON。

如果您想将 JSON 呈现为字符串,您可以使用JSON.stringify(o),它将打印:

{"name":"Bob","scores":[1,2,3],"info":{"isSet":true}}
于 2013-05-10T20:23:28.253 回答
1

利用Array.Push

var personInfo = {}; // Create the object and probably save it in 
  //localStorage or cookie etc or whatever pref you have, 
  //You can save it in `data` cache(of your HTML DOM element) too.

var peopleObj=[];
personInfo["people"] = peopleObj; //Save the array to the object onto the key/property "Person"

//Push the data as you need. 
peopleObj.push({"name":"whatever",
                 "company":"foo",
                 "email":"bar@foo.com"
               });

peopleObj.push({"name":"whatever2",
                "company":"foo",
                 "email":"bar@foo.com"});

console.log(personInfo);

演示

这是一个示例演示,说明您可能要实现的目标:- 使用 localStorage 存储骨架并在单击按钮时向其添加数据。

演示2

 var personInfo = {
      "people": []
  };

  window.localStorage.setItem('person',JSON.stringify(personInfo));
  var i = 0;
  $('input').click(function () {
     
      var obj = JSON.parse(window.localStorage.getItem('person'));
      obj["people"].push({
          "name": "whatever" + i++,
          "company": "foo",
          "email": "bar@foo.com"
      });
       window.localStorage.setItem('person',JSON.stringify(obj));
      console.log(obj);
  });
于 2013-05-10T20:24:20.257 回答
1

我建议创建一个函数来进行添加。它可以防止您在将人员添加到列表时需要复制所有添加代码。

// define your object somewhere
var someObject = {
    "people": []
};

// define a function for adding to your main object
function addPerson(name, company, email) {
    someObject.people.push({
        "name": name,
        "company": company,
        "email": email
    });
}

然后,当您想添加另一个条目时,只需调用该函数:

addPerson("Some Name", "A Company", "person@website.com");

对于这样的事情,我会说最好保持简单。

于 2013-05-10T20:27:18.373 回答
0

我可能会做类似的事情:

var Person = function (name, company, email)
{
    this.name = name;
    this.company = company;
    this.email = email;
}

var People = function ()
{
    this.people = [];

    this.add = function (person)
    {
        this.people.push(person);
    }
}

var people = new People();

people.add(new Person("john", "smith", "blah@blah.com"));

alert(JSON.stringify(people));
于 2013-05-10T20:39:48.537 回答