weixin_33722405 2013-04-07 16:48 采纳率: 0%
浏览 132

从json_encode读取数据

Im having trouble with reading data from json_encode.

i want to read my users info (user_name, x_c0rd, y_cord)

My ajax:

$.ajax({
  url: 'inc/odczyt_multi.php',
  data: "",
  dataType: 'json',
  success: function (data) {
    if (data == 0) {
      //There r no users
    }
    else {
      for (var id in data) {
        var name = data[id][2];
        var multi_x = data[id][8];
        var multi_y = data[id][9];
        alert(name + multi_x + multi_y);
      }

This is my odczyt_multi.php

$result = mysql_query('SELECT * FROM `users` ORDER BY `id`') or die(mysql_error());
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
  $rows[] = $row;
}
echo json_encode($rows);

And here is a result from odczyt_multi.php

[ { "cords" : "",
    "data" : "07.04.13",
    "email" : "ziaja@asdad.pl",
    "id" : "2",
    "kod" : "941140747",
    "pass" : "098f6bcd4621d373cade4e832627b4f6",
    "pozycja_x" : "9",
    "pozycja_y" : "6",
    "punkty_ruchu" : "1720",
    "skin" : "",
    "user" : "Ziaja"
  } ]

So there is 1 user in database.

How I can parse that data? Why my alert shout Nan instead of data of this user?

  • 写回答

1条回答 默认 最新

  • 七度&光 2013-04-07 17:14
    关注

    You should look into JSON Website for more information about reading the data.

    If you were planning on having a bunch of info returned in json format. You could for loop through them.

    for(var i=0;i<data.length;i++){
    
      Do stuff with data....
    
      data[i]['user'] //would return Ziaja
      data[i]['pass'] //would return 098f6bcd4621d373cade4e832627b4f6
    
    }
    

    I hope that is what you're talking about

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)