如何将Json数据格式化为所需的格式并循环遍历循环?

时间:2015-11-03 08:26:30

标签: javascript php jquery json ajax

我正在学习JSON,所以我对数据格式,数组,对象和字符串感到困惑。

首先,我需要这种格式的数据(在jQuery页面中)。是这个对象还是数组?

var data = [[2015-10-29 11:35:33, 315.71], [2015-10-29 11:36:33, 317.45], [2015-10-29 11:38:52, 317.50], [2015-10-29 11:42:08, 315.86], [2015-10-29 11:43:32, 314.93], [2015-10-29 11:50:00, 313.19], [2015-10-29 11:35:33, 313.34]]; 

但我是这样的(在PHP页面中)。这是字符串吗?

 var data = [{"bv ":"27590","datey ":"2015-10-29 11:35:33"},{"bv ":"27590","datey ":"2015-10-29 11:36:33"},{"bv ":"27590","datey ":"2015-10-29 11:37:31"},{"bv ":"27590","datey ":"2015-10-29 11:38:34"},{"bv ":"27590","datey ":"2015-10-29 11:38:52"},{"bv ":"27590","datey ":"2015-10-29 11:39:15"},{"bv ":"27590","datey ":"2015-10-29 11:42:08"},{"bv ":"27590","datey ":"2015-10-29 11:42:48"},{"bv ":"27590","datey ":"2015-10-29 11:43:32"},{"bv ":"27590","datey ":"2015-10-29 11:50:00"}];

当我通过ajax在jQuery页面中收到data(来自上面的PHP页面) 时,

enter image description here

我是否以正确的格式获取了我想要的[[2015-10-29 11:35:33, 315.71], [2015-10-29 11:36:33, 317.45]]数据,如何迭代这些数据?

有人帮助我理解这里涉及的数据格式以及迭代它们的方式。

我试过了:

1)

for(i=0; i < data.length; i++)
{
   alert(data.[i].datey);//says undefined
}  

2)

$.each(data, function(i, item) {
    alert(data[i].datey);//doesn't show any alert
    })  

这是我的ajax和PHP脚本供您参考:

$(function(){
  var data;
  $.ajax({
    dataType: "json",
    url: 'query_sales.php',
    success: function(data){
        alert(data);
    /*$.each(data, function(i, item) {
    alert(data[i].datey);
    });​*/
    }//close success
  });//close ajax
});//close function

PHP脚本:

$acceptedUser = new search();
$sales = $acceptedUser->get_sales_graph();
$before = array();
foreach($sales as $k=>$v)
{

    array_push($before,array("bv "=>$v['total_bv'],"datey "=>$v['as_of_date']));

}
echo json_encode($before);

2 个答案:

答案 0 :(得分:1)

您可以尝试更改此内容:

array_push($before,array("bv"=>$v['total_bv'],"datey"=>$v['as_of_date']));
//--------------------------^^---------------------^^--you have spaces in your keys

然后在成功功能中你可以这样做:

success: function(data){
    var arr = [];
    $.each(data, function(i, item){
       arr.push([item.bv, item.datey])
    });    
}

结帐以下示例:

var data = [{"bv":"27590","datey":"2015-10-29 11:35:33"},{"bv":"27590","datey":"2015-10-29 11:36:33"},{"bv":"27590","datey":"2015-10-29 11:37:31"},{"bv":"27590","datey":"2015-10-29 11:38:34"},{"bv":"27590","datey":"2015-10-29 11:38:52"},{"bv":"27590","datey":"2015-10-29 11:39:15"},{"bv":"27590","datey":"2015-10-29 11:42:08"},{"bv":"27590","datey":"2015-10-29 11:42:48"},{"bv":"27590","datey":"2015-10-29 11:43:32"},{"bv":"27590","datey":"2015-10-29 11:50:00"}];
var arr = [];
$.each(data, function(i, item){
    arr.push([+item.bv, item.datey])
});

document.body.innerHTML = '<pre>' + JSON.stringify(arr) + '</pre>';
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:1)

您的第一个var data无效。你的第二个是一组对象。

你试过了:

for(i=0; i < data.length; i++)
{
   alert(data.[i].datey);//says undefined
}  

但你的财产名称是&#34; datey&#34;,而不是&#34; datey&#34;。所以你必须像这样访问它:

for(i=0; i < data.length; i++)
{
   alert(data.[i]["datey  "]);
}  

或者更好,修复你的PHP以删除额外的空格:

array_push($before,array("bv"=>$v['total_bv'],"datey"=>$v['as_of_date']));