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

时间:2023-01-28 17:03:15

I have a problem, I wrote a function for printing message data from a JSON file, but I do not know how to remove message data with specific id and reply on message to sender? Do I use only javascript or I have to use PHP? Can somebody help me?

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

My data.json file:

我的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":[

         ]
      }
    ]
}

My jquery file:

我的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 个解决方案

#1


0  

You will try this :

你会试试这个:

First parse the json :

首先解析json:

Use var dataArray = jQuery.parseJSON(response);

Then remove your data :

然后删除您的数据:

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

从json中删除数据1从json 2中删除数据

#2


0  

You can try underscorejs, and use findWhere function like this

你可以试试下划线,并使用像这样的findWhere函数

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

}

#1


0  

You will try this :

你会试试这个:

First parse the json :

首先解析json:

Use var dataArray = jQuery.parseJSON(response);

Then remove your data :

然后删除您的数据:

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

从json中删除数据1从json 2中删除数据

#2


0  

You can try underscorejs, and use findWhere function like this

你可以试试下划线,并使用像这样的findWhere函数

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

}