如何从 JSON 对象读取数据

发布于 2024-10-15 12:27:28 字数 768 浏览 5 评论 0原文

我有一个 PHP 脚本,它从 mysql 中选择一些数据并将其放入以下行中:

$json_arr[] = array ('date'=>$human_date,'weight'=>$data['weight']);

当我使用 PHP4 运行服务器时,我正在使用 Services_JSON (http://pear.php.net/pepr/pepr-proposal-show.php?id=198) 脚本到 JSON-验证数据:

$json = new Services_JSON();
echo $json->encode($json_arr);

在JS文件中我想显示所有的权重和重量。日期:

$.getJSON('ajax/getweights.php', {userid: userid}, function(data) { 

  $.each(data, function(k, v) {
    alert(v + ': ' v);
  });
});

然而,当我运行脚本时,我没有得到想要的结果,例如2011-01-10:90、2011-01-15:92、2011-01-18:89。

我对 jQuery 相当陌生,我一直在网上寻找答案,并且试图找出如何读取这些数据 - 我希望有人可以帮助我:)

I have a PHP script that selects some data from mysql and throw it into the following line:

$json_arr[] = array ('date'=>$human_date,'weight'=>$data['weight']);

As I run of a server with PHP4 I am using the Services_JSON (http://pear.php.net/pepr/pepr-proposal-show.php?id=198) script to JSON-ify the data:

$json = new Services_JSON();
echo $json->encode($json_arr);

In the JS file I want to show all the weights & dates:

$.getJSON('ajax/getweights.php', {userid: userid}, function(data) { 

  $.each(data, function(k, v) {
    alert(v + ': ' v);
  });
});

How ever when I run the script, I dont get the wanted result, eg 2011-01-10:90,2011-01-15:92, 2011-01-18:89.

Im rather new to jQuery, and I have been searching the net for answers and I have tried to figure out how to read those data - I hope someone can help me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2024-10-22 12:27:28

假设服务器以以下格式发送数据:

[ { date: '2011-01-10:90', weight: '1' }, 
  { date: '2011-01-15:92', weight: '2' }, 
  ...
]

您可以:

$.each(data, function(k, v) {
    alert('date: ' + v.date + ' | weight: ' + v.weight);
});

Assuming the server sends the data in the following format:

[ { date: '2011-01-10:90', weight: '1' }, 
  { date: '2011-01-15:92', weight: '2' }, 
  ...
]

you could:

$.each(data, function(k, v) {
    alert('date: ' + v.date + ' | weight: ' + v.weight);
});
空城之時有危險 2024-10-22 12:27:28

实现这一点的更简单方法:

$json_arr[] = array(
  'date' => $human_date,
  'weight' => $data['weight']
);

$json = new Services_JSON();
echo 'processData(' . $json->encode($json_arr) . ')';

和 JavaScript:

// Callback function which processes the data
function processData(data) {
  for(var i = 0; i < data.length; i++)
    alert(data[i].date + ', ' + data[i].weight);
}

// Here we import the data
var s = document.createElement('script');
s.src = 'ajax/getweights.php';
document.body.appendChild(s);

Easier way to achieve this:

$json_arr[] = array(
  'date' => $human_date,
  'weight' => $data['weight']
);

$json = new Services_JSON();
echo 'processData(' . $json->encode($json_arr) . ')';

And JavaScript:

// Callback function which processes the data
function processData(data) {
  for(var i = 0; i < data.length; i++)
    alert(data[i].date + ', ' + data[i].weight);
}

// Here we import the data
var s = document.createElement('script');
s.src = 'ajax/getweights.php';
document.body.appendChild(s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文