如何从json检索数据并作为数组发送回ajax帖子

时间:2017-09-14 20:35:31

标签: jquery json ajax

我有一个jquery代码,可以在页面加载或调用时生成json。如何只读取时间戳并将其放入数组发送回json?请查看我在单个文件上运行的示例代码,但是当返回的json不止一个时,它将停止工作,为什么我需要始终将时间戳作为数组发布,以便我可以在php部分处理其余部分。

<script>
var mid = StreamId(); ////100==101 i did the explode in php side
var ids = StreamId().split('=='); //100==101 // then i split it
var timestamp = null;
function Streaming(notice){

    $.ajax({
        type: "POST",
        url: EventUrl('loader/page'),
        data: {mid : mid, timestamp : timestamp}, // i want this timestamp to be an array so once data is returned i can pass it again to timestamp and send back
        async: true,
        cache: false,
        success: function(data){
        var id;
       var arr = [];
        var chat = eval('('+data+ ')');
        for(var i = 0; i < ids.length; i++){ // i loop to match the ids with returned json data
            id = ids[i];
            alert(chat[id]['chatid']);
            if (chat[id]['chatid'] != "") {
                if(notice == true){
                    if(chat[id]['type'] != initiate_sender()){
                        $("#notification_sound").get(0).play();
                    }
                }
            }
            //Here i wish to get all the value of returned json then put it in array to send it back
            //timestamp = chat[id]["timestamp"];
           arr[i++] = [ chat[id]["timestamp"], ]; // i need to join all the returned timestamp 
           timestamp = JSON.stringify(arr); // then pass it as json stringify
        }

        mid = StreamId();
        setTimeout("Streaming(true)", 1000);
        },
        error: function(XMLHttpRequest,textStatus,errorThrown) {
         console.log("error: "+textStatus + "  "+ errorThrown);
        setTimeout("Streaming(true)", 15000);
        }
    });
}
$(document).ready(function(){
    Streaming(false);
});
</script>

我的json输出看起来像这样

{  
   "100":{  
      "sid":"58208469",
      "type":"client",
      "timestamp":1505419859
   }
},
{  
   "101":{  
      "sid":"53094615",
      "type":"admin",
      "timestamp":1505419733
    }
}

1 个答案:

答案 0 :(得分:0)

使用花括号并确保索引为字符串

 <script>
    var mid = StreamId(); ////100==101 i did the explode in php side
    var ids = StreamId().split('=='); //100==101 // then i split it
    var timestamp = null;
    function Streaming(notice){

$.ajax({
    type: "POST",
    url: EventUrl('loader/page'),
    data: {mid : mid, timestamp : timestamp}, // i want this timestamp to be an array so once data is returned i can pass it again to timestamp and send back
    async: true,
    cache: false,
    success: function(data){
    var id;
   var arr = {};
    var chat = eval('('+data+ ')');
    for(var i = 0; i < ids.length; i++){ // i loop to match the ids with returned json data
        id = ids[i];
        alert(chat[id]['chatid']);
        if (chat[id]['chatid'] != "") {
            if(notice == true){
                if(chat[id]['type'] != initiate_sender()){
                    $("#notification_sound").get(0).play();
                }
            }
        }
        //Here i wish to get all the value of returned json then put it in array to send it back
        //timestamp = chat[id]["timestamp"];
       //i++; 
       arr[(i+'')] = {"timestamp":chat[id]["timestamp"]}; // i need to join all the returned timestamp 
       timestamp = JSON.stringify(arr); // then pass it as json stringify
    }

    mid = StreamId();
    setTimeout("Streaming(true)", 1000);
    },
    error: function(XMLHttpRequest,textStatus,errorThrown) {
     console.log("error: "+textStatus + "  "+ errorThrown);
    setTimeout("Streaming(true)", 15000);
    }
    });
}
$(document).ready(function(){
    Streaming(false);
});
</script>