如何添加附加到此嵌套的JSON对象

时间:2012-01-02 12:07:17

标签: javascript json

我的JSON对象是

"msg"=[{"userName":"Mandy","emailId":"m@t.co","userCreated":"2011-12-21 17:21:49","allowedDownloads":"15"},{"userName":"Andy","emailId":"ab@r.co","userCreated":"2011-12-21 17:29:58","allowedDownloads":"45"},{"userName":"Randy","emailId":"re@t.co","userCreated":"2012-01-02 10:18:19","allowedDownloads":"15"},{"userName":"Vandy","emailId":"vai@t.co","userCreated":"2012-01-02 15:49:20","allowedDownloads":"14"},{"userName":"Sandy","emailId":"vrush@t.co","userCreated":"2012-01-02 16:47:35","allowedDownloads":"14"}]

1)如何添加1个人,以便“msg”附加

{"userName":"Wendy","emailId":"w@t.co","userCreated":"2012-12-21 17:21:49","allowedDownloads":"15"}

2)如何为每个索引添加一个属性“爱好”,以便我拥有,例如<​​/ p>

{"userName":"Wendy","emailId":"w@t.co","userCreated":"2012-12-21 17:21:49","allowedDownloads":"15","hobbies":"skiing,football,hockey"}

3)如何判断“Wendy”指数是否具有业余爱好“曲棍球”?

2 个答案:

答案 0 :(得分:1)

因为你有一个Javascript数组

  var msg = [{"userName":"Mandy","emailId":"m@t.co","userCreated":"2011-12-21 17:21:49","allowedDownloads":"15"},
             {"userName":"Andy","emailId":"ab@r.co","userCreated":"2011-12-21 17:29:58","allowedDownloads":"45"},
             {"userName":"Randy","emailId":"re@t.co","userCreated":"2012-01-02 10:18:19","allowedDownloads":"15"},
             {"userName":"Vandy","emailId":"vai@t.co","userCreated":"2012-01-02 15:49:20","allowedDownloads":"14"},
             {"userName":"Sandy","emailId":"vrush@t.co","userCreated":"2012-01-02 16:47:35","allowedDownloads":"14"}];

您可以轻松推送新元素

  msg.push({"userName":"Wendy","emailId":"w@t.co","userCreated":"2012-12-21 17:21:49","allowedDownloads":"15"});

但是要更新你必须循环的记录

  function update(username, property, value){

      for(var i=0; i < msg.length; i++){
          var user = msg[i];
          if(user["userName"] == username){
              user[property] = value;
              break;
          }          
      }

  }

要搜索也必须循环

  function check(username, property, value){

        for(var i=0; i < msg.length; i++){
            var user = msg[i];
            if(user["userName"] == username){
                var propertyVal = user[property];
                if( propertyVal && propertyVal.indexOf(value) != -1){
                    return true;
                }
            }          
        }
        return false;
    }

答案 1 :(得分:1)

1)您可以添加一条记录:

data.push({"userName" : "Smit","emailId":"smit@example.com","userCreated":"2011-12-21 17:29:58","allowedDownloads":"9"});

2)您可以添加“爱好”,如:

for(a in data)
{
    data[a].hobbies = "skiing,football,hockey";
}

3)对于最后一个问题,您可以创建一个函数。我对javascript不太满意,所以除此之外可能还有其他一些选择。但是你可以从这段代码开始:

function getHobbey(userName, hobbey_name)
{
    for(a in data)
    {
        if (data[a].userName == userName)
        {
            var hb = data[a].hobbies;
            if (hb != '')
            {
                all_hb = hb.split(",");
                for(i=0; i<= all_hb.length; i++)
                {
                    if (all_hb[i] == hobbey_name)
                    {
                        return true;
                    }
                }
                return false;
            }
            return false;
        }            
    }
}

通过调用

alert(getHobbey("Smit","skiing"));

会给你真假。

还有很多东西可以改进这个功能。

谢谢!