dousou2911 2015-10-24 19:41
浏览 90

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

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?

My data.json file:

{
   "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:

 $(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条回答 默认 最新

  • dqcuq4138 2015-10-24 20:05
    关注

    You will try this :

    First parse the 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

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?