gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:29:49 24 4
gpt4 key购买 nike

我正在研究 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 页面中接收数据(来自上面的 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);

最佳答案

你可以尝试改变这个:

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>

关于javascript - 如何将 Json 数据格式化为所需的格式并遍历循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33494404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com