Decode JSON object in jquery

非 Y 不嫁゛ 提交于 2019-12-13 06:17:58

问题


I have encoded data in json format by php using the following code

  <?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);

?>

I have to decode this json in jquery for that the i have used this method

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);     
      }  
  });

but this is not working. please help me to get this correct

result is getting in console in this format. how to iterate through this ?.

{"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}

回答1:


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);     
      }  
  });



回答2:


Because your array is not single array. it's an associative array(kind of dictionary).

what you did is you assign array element by index to get user info but how about $response["average_age"] and $response["count"]? how can you get name,age,date info from this?

you should have to pass two array one for users objects like $users and another for your rest response.




回答3:


So, first of all, you need to convert JSON to an array to be able to get the length. So, try:

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);



回答4:


try this

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);
}


来源:https://stackoverflow.com/questions/22578968/decode-json-object-in-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!