javascript - 使用 JQUERY 过滤 JSON 数据

标签 javascript jquery json format filtering

我在使用 jquery 和 JSON 过滤数据时遇到问题。我正在尝试删除每个发件人 ID 为“234”或名称为“Alan Ford”的对象。因为我想制作一个由收件箱/发件箱分隔的消息传递系统,并且由于 Alan Ford 是发送者,所以我需要在不直接篡改 JSON 的情况下将它们分开。

代码如下:

$(document).ready(function(){
    $.getJSON('public/js/data.json', function(json){
        $.each(json.data, function(i, data){
        if(data.from.id == "234"){         //if is not working correctly
                $("p").remove();
                $("h1").remove();
                $("h2").remove();
                $("hr").remove()}
            $("#inbox").append(
            '<div class="post">'+
            'To: '+data.to.name+''+
            ''+data.subject+''+
            ''+data.message_formatted+''+
            'added: '+data.date_sent_formatted.formatted+''+
            'Sender: '+data.from.name+''+
            '<hr />'+
        '</div>'
    );
});
    });

});

和 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":[

         ]
      },
      {
         "id":"2047",
         "from":{
            "id":"5245",
            "name":"Ian Graham"
         },
         "to":{
            "id":"234",
            "name":"Alan Ford"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1343234342",
         "date_read":"1343837983",
         "subject":"re: This is SP",
         "message":"Hi How are you[quote=Alan Ford]This is SP..Thais is SP[\/quote]",
         "message_formatted":"Hi How are you",
         "date_sent_formatted":{
            "id":1183,
            "timestamp":1343174400,
            "month":7,
            "day":25,
            "year":2012,
            "week":30,
            "dayid":4,
            "weekday":"Wed",
            "mname":"Jul",
            "formatted":"Jul 25, 2012"
         },
         "date_read_formatted":{
            "id":1190,
            "timestamp":1343779200,
            "month":8,
            "day":1,
            "year":2012,
            "week":31,
            "dayid":4,
            "weekday":"Wed",
            "mname":"Aug",
            "formatted":"Aug 1, 2012"
         }
      },
      {
         "id":"2046",
         "from":{
            "id":"5245",
            "name":"Ian Graham"
         },
         "to":{
            "id":"234",
            "name":"Alan Ford"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1343232908",
         "date_read":"1344001216",
         "subject":"Hello",
         "message":"Hi",
         "message_formatted":"Hi",
         "date_sent_formatted":{
            "id":1183,
            "timestamp":1343174400,
            "month":7,
            "day":25,
            "year":2012,
            "week":30,
            "dayid":4,
            "weekday":"Wed",
            "mname":"Jul",
            "formatted":"Jul 25, 2012"
         },
         "date_read_formatted":{
            "id":1192,
            "timestamp":1343952000,
            "month":8,
            "day":3,
            "year":2012,
            "week":31,
            "dayid":6,
            "weekday":"Fri",
            "mname":"Aug",
            "formatted":"Aug 3, 2012"
         }
      },
      {
         "id":"2043",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5399",
            "name":"Kob Bryant"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1342560365",
         "date_read":"0",
         "subject":"asda",
         "message":"ASDASDAS",
         "message_formatted":"ASDASDAS",
         "date_sent_formatted":{
            "id":1176,
            "timestamp":1342569600,
            "month":7,
            "day":18,
            "year":2012,
            "week":29,
            "dayid":4,
            "weekday":"Wed",
            "mname":"Jul",
            "formatted":"Jul 18, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2042",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5226",
            "name":"1341478142_Kareena kapoor"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1341364262",
         "date_read":"0",
         "subject":"adaasd",
         "message":"asdadadas",
         "message_formatted":"asdadadas",
         "date_sent_formatted":{
            "id":1162,
            "timestamp":1341360000,
            "month":7,
            "day":4,
            "year":2012,
            "week":27,
            "dayid":4,
            "weekday":"Wed",
            "mname":"Jul",
            "formatted":"Jul 4, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2041",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5226",
            "name":"1341478142_Kareena kapoor"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1341091202",
         "date_read":"0",
         "subject":"asdasd",
         "message":"asdasfagfsdgsgsgasg",
         "message_formatted":"asdasfagfsdgsgsgasg",
         "date_sent_formatted":{
            "id":1159,
            "timestamp":1341100800,
            "month":7,
            "day":1,
            "year":2012,
            "week":26,
            "dayid":1,
            "weekday":"Sun",
            "mname":"Jul",
            "formatted":"Jul 1, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2040",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5226",
            "name":"1341478142_Kareena kapoor"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1341091171",
         "date_read":"0",
         "subject":"asdad",
         "message":"adasdasdada",
         "message_formatted":"adasdasdada",
         "date_sent_formatted":{
            "id":1159,
            "timestamp":1341100800,
            "month":7,
            "day":1,
            "year":2012,
            "week":26,
            "dayid":1,
            "weekday":"Sun",
            "mname":"Jul",
            "formatted":"Jul 1, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2039",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5226",
            "name":"1341478142_Kareena kapoor"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1341091049",
         "date_read":"0",
         "subject":"Events",
         "message":"Good to hear that.",
         "message_formatted":"Good to hear that.",
         "date_sent_formatted":{
            "id":1159,
            "timestamp":1341100800,
            "month":7,
            "day":1,
            "year":2012,
            "week":26,
            "dayid":1,
            "weekday":"Sun",
            "mname":"Jul",
            "formatted":"Jul 1, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2038",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"4982",
            "name":"Hamza Nadeem"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1341053992",
         "date_read":"0",
         "subject":"adfads",
         "message":"agsgsggasgasgasas",
         "message_formatted":"agsgsggasgasgasas",
         "date_sent_formatted":{
            "id":1158,
            "timestamp":1341014400,
            "month":6,
            "day":30,
            "year":2012,
            "week":26,
            "dayid":7,
            "weekday":"Sat",
            "mname":"Jun",
            "formatted":"Jun 30, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2037",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"4949",
            "name":"Eric Owens"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340985832",
         "date_read":"0",
         "subject":"test",
         "message":"testtest",
         "message_formatted":"testtest",
         "date_sent_formatted":{
            "id":1157,
            "timestamp":1340928000,
            "month":6,
            "day":29,
            "year":2012,
            "week":26,
            "dayid":6,
            "weekday":"Fri",
            "mname":"Jun",
            "formatted":"Jun 29, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2036",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5730",
            "name":"Mobile Employee"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2035",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5735",
            "name":"Fsdfsdghh"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2034",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5743",
            "name":"double dots3"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2033",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5742",
            "name":"double dots2"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2032",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5734",
            "name":"Adalph"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2031",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5731",
            "name":"Mobile Scheduler"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2030",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"4949",
            "name":"Eric Owens"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2029",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5628",
            "name":"Test email"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2028",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5627",
            "name":"1341059685_dejan email test"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2027",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5625",
            "name":"Test"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2026",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5623",
            "name":"El Homo"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2025",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5618",
            "name":"Tiengo Mass"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2024",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5608",
            "name":"1344517115_Test22"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      },
      {
         "id":"2023",
         "from":{
            "id":"234",
            "name":"Alan Ford"
         },
         "to":{
            "id":"5606",
            "name":"1341474388_sgdfgdfg"
         },
         "type":"1",
         "replyto":"0",
         "date_sent":"1340878624",
         "date_read":"0",
         "subject":"This is SP",
         "message":"This is SP..Thais is SP",
         "message_formatted":"This is SP..Thais is SP",
         "date_sent_formatted":{
            "id":1156,
            "timestamp":1340841600,
            "month":6,
            "day":28,
            "year":2012,
            "week":26,
            "dayid":5,
            "weekday":"Thu",
            "mname":"Jun",
            "formatted":"Jun 28, 2012"
         },
         "date_read_formatted":[

         ]
      }
   ]
}

最佳答案

如果您无论如何都需要循环遍历 JSON 以打印消息,我不明白您为什么要从 JSON 中删除任何内容(至少这是您的样本让我想到的)。

所以我只是将这个条件添加到您的 JS 中:

$.each(json.data, function(i, data){
    if(data.from.id != 234 && data.from.name != 'Alan Ford'){
        $("#inbox").append(
            '<div class="post">'+
            'To: '+data.to.name+''+
            ''+data.subject+''+
            ''+data.message_formatted+''+
            'added: '+data.date_sent_formatted.formatted+''+
            'Sender: '+data.from.name+''+
            '<hr />'+
            '</div>'
        );
    }
});​

此外,您可能希望通过自己替换危险字符或使用 .text() 来正确转义正在打印的每个变量。来自 jQuery 的函数。


最后的建议:编辑 DOM 需要时间,所以如果您计划处理大量消息,最好只附加一次:

var output = '';
$.each(json.data, function(i, data){
    if(data.from.id != 234 && data.from.name != 'Alan Ford'){
        output +=
            '<div class="post">'+
            'To: '+data.to.name+''+
            ''+data.subject+''+
            ''+data.message_formatted+''+
            'added: '+data.date_sent_formatted.formatted+''+
            'Sender: '+data.from.name+''+
            '<hr />'+
            '</div>';
    }
});
$("#inbox").append(output);​

事情是这样的:http://jsfiddle.net/pioul/Hy2nd/

关于javascript - 使用 JQUERY 过滤 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12238072/

相关文章:

javascript - 如何在用户点击图片时执行javascript函数

javascript - 我怎样才能使一个函数在整个 jQuery 插件及其所有内部方法中可用?

java - Spring如何为@RestController自动将对象转换为json

python - 使用 json.dumps 和 ensure_ascii=True

javascript - ng-click Angular JS 问题

javascript - 使用 javascript 获取第二个 li 标记中的值

javascript - 在 AngularJS 中创建映射(键,值)结构并填充 JSON 中的数据

javascript - 样式化选择缩略图

Jquery Analytics 跟踪 onclick NS_BINDING_ABORTED

c# - 从 SqlDataReader 转换为 JSON