0

我正在从数据库中检索值并将其解析为 JSON。我的 JSON 数据格式就像..

[{"INCOMING":"09:09:49","INETCALL":"00:14:09","ISD":"00:05:50","LOCAL":"02:38:02","STD":"01:39:28"}]

我想将此 JSON 值分解为两个变量,一个是:

var toc=["INCOMING","INETCALL","ISD","LOCAL","STD"]

和像这样的另一个变量..

var callduration=["09:09:49","00:14:09","00:05:50","02:38:0","01:39:28"]

现在根据我的需要,我必须将它分成两个变量,因为我已经编写了一个 for 循环,但它不起作用。这是我的客户端代码..

$.ajax({
                type: 'GET',
                url: 'getdataduration',
                async:false,
                dataType: "text",
                success: function(data) {

                    console.log(data);
                    var dbdata=JSON.parse(data);
                    console.log(dbdata);
                    for(var i=0,len=dbdata.length;i<len;i++){
                        $.isNumeric(dbdata[i]) ?  callduration.push(dbdata[i]) :  toc.push(dbdata[i]);
                    }

                }

            });

            console.log(toc);
            console.log(callduration);

请大家帮帮我。提前致谢..

4

2 回答 2

0

这就是你将如何工作......

var data = [{"INCOMING":"09:09:49","INETCALL":"00:14:09","ISD":"00:05:50","LOCAL":"02:38:02","STD":"01:39:28"}];

console.log(data); 

console.log( data[0].INCOMING );     // 09:09:49 
console.log( data[0]['INCOMING'] );  // 09:09:49 

var keys = Object.keys(data[0]);

console.log(keys);    // ["INCOMING", "INETCALL", "ISD", "LOCAL", "STD"] 


var values = [];
var keys   = [];

$.each(data,function(i,val){
   console.log(val);     // Object {INCOMING: "09:09:49", INETCALL: "00:14:09", ISD: "00:05:50", LOCAL: "02:38:02", STD: "01:39:28"} 

   $.each(val,function(key2,val2){
      console.log(key2 + " = " + val2); 

      keys.push(key2); 
      values.push(val2); 

      // INCOMING = 09:09:49  
      // INETCALL = 00:14:09 
      // ISD = 00:05:50 
      // LOCAL = 02:38:02  
      // STD = 01:39:28  
   }); 
});

console.log(keys);     // ["INCOMING", "INETCALL", "ISD", "LOCAL", "STD"] 
console.log(values);   // ["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"] 
于 2013-11-09T04:26:44.247 回答
0

你可以试试这个,

 $(function(){

     var toc = [];
     var callduration =[];
     $.ajax({
         type: 'GET',
         url: 'getdataduration',
         async:false,
         dataType: "text",
         success: function(data) {
          //   console.log(data);
             var dbdata=JSON.parse(data);
            // console.log(dbdata);                
            $.each(dbdata[0], function(key, value){
                console.log(key, value);
                toc.push(key);
                callduration.push(value)
            });                            

         }

     });

     console.log(toc);
     console.log(callduration);

 });

输出控制台:

   ["INCOMING", "INETCALL", "ISD", "LOCAL", "STD"]

   ["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"]
于 2013-11-09T05:03:45.657 回答