1

我想要以下结构:

    var data={"users":[
    {
        "firstName":"Ray",
        "lastName":"Villalobos",
        "joined":2012
    },
    {
        "firstName":"John",
        "lastName":"Jones",
        "joined":2010
    }
    ]}

我希望能够以编程方式向其中添加条目。这是我尝试过的:

  var data = [];
   data.push({
     "firstName":"Johsssn",
     "lastName":"Jossnes",
     "joined":2010
   });
4

3 回答 3

7

你的数组实际上是data.users,不是data
所以改用这个:

var data = {users: []};
data.users.push({
  "firstName":"Johsssn",
  "lastName":"Jossnes",
  "joined":2010
});
于 2013-06-26T16:22:33.880 回答
0
    var data = {users: []};
    data.users.push({
         "firstName":"Johsssn",
         "lastName":"Jossnes",
         "joined":2010
    });
于 2013-06-26T16:23:17.677 回答
0

您的data对象包含一个属性 ,users它是一组用户,因此您需要将其推入:

data.users.push({
    "firstName":"Johsssn",
    "lastName":"Jossnes",
     "joined":2010
});
于 2013-06-26T16:23:39.553 回答