在jquery中解码JSON对象

时间:2014-03-22 14:36:42

标签: jquery decode json

我使用以下代码

通过php编码了json格式的数据
  <?php

    $response = array();

    while ($row = mysql_fetch_array($result)) {

            $user["id"]                 = $row["id"];
            $user["name"]               = ucfirst($row["user_name"]);
            $user["date"]               = $row["date_of_treatment"];
            $user["age"]                = $row["age_of_user"];

            // push single user into final response array
            array_push($response, $user);

            $count = $count+1;
            $sum_of_age = $sum_of_age+$row["age_of_user"];

        }

    $response["average_age"]  = $sum_of_age / $count;
    $response["count"] = $count;

    echo json_encode($response);

?>

我必须在 jquery 中解码这个json 因为我已经使用了这种方法

success: function(result){  

      if(result.length > 0) {      
          for(var i=0; i < result.length; i++) {
            obj = result[i];

            output = output + "<tr><td>"+(i+1)+"</td><td>"+obj.name+"</td><td>"+obj.age+"</td><td>"+obj.date+"</td><tr>";
          } 
          output = output+"<tr><td colspan='2' style='text-align:center'>"+obj.average_age+"</td></tr>"
        } else {
          output = output + "<tr><td colspan='4' style='text-align:center'>No Records Found..!</td></tr>";
        }
         $("#search-list tbody").html(output);     
      }  
  });

但这不起作用。请帮我弄清楚

结果是以这种格式进入控制台。如何迭代这个?。

{"0":{"id":"35","name":"Ahamed shajeer","date":"2014-03-03","age":"25"},"1":{"id":"36","name":"Meshajeer","date":"0000-00-00","age":"25"},"2":{"id":"37","name":"Iam shajeer","date":"0000-00-00","age":"25"},"average_age":25,"count":3}

4 个答案:

答案 0 :(得分:0)

success: function(result){  
         var users = result.users;

if(users.length > 0) { 

      for(var i=0; i < users.length ; i++) {
        obj = users[i];

        output = output + "<tr><td>"+(i+1)+"</td><td>"+obj.name+"</td><td>"+obj.age+"</td><td>"+obj.date+"</td><tr>";
      } 
      output = output+"<tr><td colspan='2' style='text-align:center'>"+result.average_age+"</td></tr>"
    } else {
      output = output + "<tr><td colspan='4' style='text-align:center'>No Records Found..!</td></tr>";
    }


       $("#search-list tbody").html(output);     
      }  
  });

答案 1 :(得分:0)

因为您的数组不是单个数组。它是一个关联数组(一种字典)。

你所做的是你通过索引分配数组元素来获取用户信息,但$response["average_age"]$response["count"]怎么样?你如何从中获得姓名,年龄,日期信息?

你应该为$users之类的用户对象传递两个数组,为你的其他回复传递另一个数组。

答案 2 :(得分:0)

因此,首先,您需要将JSON转换为数组才能获得长度。所以,试试:

success: function(result){
    var resultArr = $.parseJSON(result);
    alert (resultArr.length); //3
    //proceed with for loop here
    ....

PHP

$response["average_age"]  = $sum_of_age / $count;
$response["count"] = $count;
header('Content-type: application/json');
echo json_encode($response);

答案 3 :(得分:0)

试试这个

success: function(data) {
    var output = "";
    if (data.length > 0) {
        $.each(data, function(index, item) {
            output += "<tr><td>" + (i + 1) + "</td><td>" + item.name + "</td><td>" + item.age + "</td><td>" + item.date + "</td><tr>";
            output += "<tr><td colspan='2' style='text-align:center'>" + item.average_age + "</td></tr>";
        });
    } else {
        output += "<tr><td colspan='4' style='text-align:center'>No Records Found..!</td></tr>";
    }
    $('#search-list tbody').html(output);
}