简体   繁体   English

遍历JSON数组

[英]Iterate through a JSON array

I just got a jQuery AJAX call to function and now I am having trouble parsing the value that I am returning. 我刚收到一个jQuery AJAX函数调用,现在我在解析返回的值时遇到了麻烦。 I am working with a mysql database and returning an php array() like so to my jQuery AJAX function: echo json_encode($reservationArray); 我正在使用mysql数据库并向我的jQuery AJAX函数返回一个php array()echo json_encode($reservationArray);

Now, when I append this to my page in a simple div tag this is the result: 现在,当我使用简单的div标签将其附加到我的页面时,结果如下:

[{"reservation_id":"3","available":"0","lock":"0","restaurant":"2","date_made":"2013-12-09 18:39:52","date_of":"2014-01-02 00:00:00","time":"08:30:00","guests":"5","user":"0"},{"reservation_id":"4","available":"0","lock":"0","restaurant":"2","date_made":"2013-12-09 18:40:15","date_of":"2014-01-02 00:00:00","time":"08:00:00","guests":"7","user":"0"}]

I believe this is a proper JSON (Please, correct me if I am wrong). 我相信这是正确的JSON(如果我输入错误,请纠正我)。 I have tried just about every method of accessing the data and cannot. 我已经尝试了几乎所有访问数据的方法,但不能。 Below is the approach that I am taking, to construct a new block of code, a bunch of rows - one per reservation (I have shortened the string for this example). 下面是我正在采用的方法,用于构造新的代码块,一堆行-每个预留一个(在此示例中,我缩短了字符串)。

EDITED: 编辑:

function my_ajax(rest_id){
    $.ajax({
      url: 'change_restaurant.php',
      type: 'POST',
      data: {'action': 'get-reservations', 'rest_id': rest_id},
      cache: false,
      success: function(json) {
       $.each(json, function(i, item) {
        if(typeof item == 'object') {
        newhtml += '<div>Restaurant Name :'+item.reservation_id+' Reservation Date: '+item.restaurant+'</div>'
        } 
        else {
          return false;
        }
      })

      $('#reservation-table').append(newhtml);

      },
      error: function(xhr, desc, err) {
        console.log(xhr + "\n" + err);
      }
    });

  }

I believe this is a proper JSON (Please, correct me if I am wrong). 我相信这是正确的JSON(如果我输入错误,请纠正我)。

It seems that the JSON your receiving is not the correct document. 您收到的JSON似乎不是正确的文档。 As Pointy said in the comments, nowhere are there fields "name" or "date". 正如Pointy在评论中所说,没有地方“名称”或“日期”。

[{
        "reservation_id" : "3",
        "available" : "0",
        "lock" : "0",
        "restaurant" : "2",
        "date_made" : "2013-12-09 18:39:52",
        "date_of" : "2014-01-02 00:00:00",
        "time" : "08:30:00",
        "guests" : "5",
        "user" : "0"
    }, {
        "reservation_id" : "4",
        "available" : "0",
        "lock" : "0",
        "restaurant" : "2",
        "date_made" : "2013-12-09 18:40:15",
        "date_of" : "2014-01-02 00:00:00",
        "time" : "08:00:00",
        "guests" : "7",
        "user" : "0"
    }
]

You need to modify your PHP code to perform a query that joins data from reservations and restaurants . 您需要修改PHP代码以执行将reservationsrestaurants数据联接起来的查询。

You also have not instructed jQuery to expect JSON in return. 您还没有指示jQuery返回JSON。 It may be parsing as string. 它可能被解析为字符串。 Note the dataType option. 注意dataType选项。

function my_ajax(rest_id){
    $.ajax({
      url: 'change_restaurant.php',
      type: 'POST',
      data: {'action': 'get-reservations', 'rest_id': rest_id},
      dataType: "json", // EXPECT JSON
      cache: false,
      success: function(json) {
       $.each(json, function(i, item) {
        if(typeof item == 'object') {
        newhtml += '<div>Restaurant Name :'+item.reservation_id+' Reservation Date: '+item.restaurant+'</div>'
        } 
        else {
          return false;
        }
      })

      $('#reservation-table').append(newhtml);
      ,
      error: function(xhr, desc, err) {
        console.log(xhr + "\n" + err);
      }
    });

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM