如何使用jquery删除复杂的json文件并将其放入复杂的json文件?

时间:2015-10-24 19:41:58

标签: javascript php jquery json ajax

我有一个问题,我写了一个从JSON文件打印消息数据的函数,但我不知道如何删除具有特定ID的消息数据并回复发送给消息的消息?我只使用javascript还是必须使用PHP?有人可以帮帮我吗?

我的data.json文件:

{
   "data":[
      {
         "id":"2146",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"4949",
            "name":"Eric Owens"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1344359836",
         "date_read":"0",
         "subject":"test",
         "message":"test inbox",
         "message_formatted":"test inbox",
         "date_sent_formatted":{
            "id":1196,
            "timestamp":1344297600,
            "month":8,
            "day":7,
            "year":2012,
            "week":32,
            "dayid":3,
            "weekday":"Tue",
            "mname":"Aug",
            "formatted":"Aug 7, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2048",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"8110",
            "name":"Event"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1343248577",
         "date_read":"0",
         "subject":"afd",
         "message":"asdfads",
         "message_formatted":"asdfads",
         "date_sent_formatted":{
            "id":1184,
            "timestamp":1343260800,
            "month":7,
            "day":26,
            "year":2012,
            "week":30,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jul",
            "formatted":"Jul 26, 2012"
         },
         "date_read_formatted":[

         ]
      }
    ]
}

我的jquery文件:

 $(document).ready(function(){

    $.getJSON('public/js/data.json', function(json){


        var msg = json.data

        for ( i = 0; i < msg.length; i++ ) {


                    var  content =  '<li>';
                         content += '<span class="left">' + msg[i].from.name +'</span>';
                         content += '<span class="right">'+ msg[i].date_sent_formatted.formatted +'</span>';
                         content += '<p>' + msg[i].subject + '</p>';
                         content += '<p>' + msg[i].message + '</p>';
                         content += '<button>Replay</button>';
                         content += '<button>Delete</button>';
                         content += '</li>';


                    $('.content').append(content);
        }

    });


});


function delete_message(id){


}

function reply_message(id, sender){

}

2 个答案:

答案 0 :(得分:0)

你会试试这个:

首先解析json:

Use var dataArray = jQuery.parseJSON(response);

然后删除您的数据:

There are several ways. The splice method is the most versatile:

data.items.splice(1, 3); // Removes three items starting with the 2nd,
                         // ("Witches of Eastwick", "X-Men", "Ordinary People")

Remove data from json 1 Remove data from json 2

答案 1 :(得分:0)

您可以尝试使用underscorejs,并使用像这样的findWhere函数

function reply(id,aReply){
the_message = _.findWhere(data.data, {id: id});
the_message.message = aReply;
//do your thing

}