如何在jquery $ .each中显示json数据

时间:2016-03-19 10:51:30

标签: javascript php jquery json

我正在使用$.each循环来获取json响应,我来到这里

["errors",[
        {"element":"country_name","error":"Value is required and can't be empty"},
        {"element":"dial_code","error":"Value is required and can't be empty"},    
        {"element":"country_code","error":"Value is required and can't be empty"}
    ]
]

$.each(response.errors, function (i, field) {
    alert(field);
});

但我没有得到任何警报。任何人都可以告诉我们如何实现这一目标

新更新

实际上我正在使用zend框架并转换一个名为$ messages的数组,看起来像这样

Array
(
[country_name] => Array
(
[isEmpty] => Value is required and can't be empty
)

[dial_code] => Array
(
[isEmpty] => Value is required and can't be empty
)

[country_code] => Array
(
[isEmpty] => Value is required and can't be empty
)

)

然后我将这个数组转换为我真正需要的那个

<?php 
foreach ($messages as $key => $value) {

$errors[] = array("element"=>$key,"error"=>$value['isEmpty']);
}
?>

然后我将其转换为json编码并返回成功函数

<?php
$response = $this->getResponse();
$response->getHeaders()->addHeaderLine( 'Content-Type', 'application/json' );
$response->setContent(json_encode(array("errors",$errors)));
return $response;
?>

4 个答案:

答案 0 :(得分:3)

你的json应该像:

var response={"errors":[
   {"element":"country_name","error":"Value is required and can't be empty"},
   {"element":"dial_code","error":"Value is required and can't be empty"},    
   {"element":"country_code","error":"Value is required and can't be empty"}]
}

从现在开始,你的json包含两列,首先包含单词errors,第二列包含3个对象。

&#13;
&#13;
var response={"errors":[
  {"element":"country_name","error":"Value is required and can't be empty"},
  {"element":"dial_code","error":"Value is required and can't be empty"},    
  {"element":"country_code","error":"Value is required and can't be empty"}]
};

$.each(response.errors, function (i, field) {
  console.log(field);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

如果您不想更改JSON结构,可以改为定位第二列response[1]

&#13;
&#13;
var response=["errors",[
  {"element":"country_name","error":"Value is required and can't be empty"},
  {"element":"dial_code","error":"Value is required and can't be empty"},    
  {"element":"country_code","error":"Value is required and can't be empty"}
]];

$.each(response[1], function (i, field) {
  console.log(field);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

JSON回复:

[
  "errors",
  [
    {"element":"country_name","error":"Value is required and can't be empty"},
    {"element":"dial_code","error":"Value is required and can't be empty"},    
    {"element":"country_code","error":"Value is required and can't be empty"}
  ]
]

是两个元素的数组。第一个元素是字符串"errors",第二个元素是对象数组。所以,你应该循环遍历响应的第二个元素(即response[1]),如下所示:

$.each(response[1], function (i, field) {
    console.log(field);
});

另外,我建议您使用console.log()代替alert()进行调试。

有关运行示例,请参阅JS Fiddle

答案 2 :(得分:1)

我认为你的json有问题可以试试下面的代码吗?

$(function(){
  var response = {"errors":
  [{
    "element": "country_name",
    "error": "Value is required and can't be empty"
  },
  {
    "element": "dial_code",
    "error": "Value is required and can't be empty"
  },
  {
    "element": "country_code",
    "error": "Value is required and can't be empty"
  }]}

  $.each(response.errors, function (index, piece) {
    $.each(piece,function(key,value){
        alert(key+"--"+value);
    })
  });
});

这里有一个工作示例:https://jsfiddle.net/hh3r7y36/

这是你的PHP代码的更新,你的输出应该是那样的

<?PHP
    //Create new object
    $responseObject = new stdClass();
    //Create new object property as an array which means you are going to output "errors" as an json key not an array item
    $responseObject->errors = array();
    //Populate the response
    foreach ($messages as $key => $value) {
        $responseObject->errors[] = array("element"=>$key,"error"=>$value['isEmpty']);
    }

    //Create output
    $response->setContent(json_encode($responseObject));
?>

希望这有帮助

答案 3 :(得分:0)

你在这里做什么:

$response->setContent(json_encode(array("errors",$errors)));

将导致JSON:

["errors",[
        {"element":"country_name","error":"Value is required and can't be empty"},
        {"element":"dial_code","error":"Value is required and can't be empty"},    
        {"element":"country_code","error":"Value is required and can't be empty"}
    ]
]

哪个有效但有点不寻常。要访问您的错误数组,您可以执行以下操作:

var response = data; // data is JSON from server
$.each(response[1], function (i, field) {
  console.log(field); // alert(field);
});
// response[0] is string "errors"
// response[1] is array of error message objects

如果您希望JSON看起来像这样:

{
  "errors":[
     {"element":"country_name","error":"Value is required and can't be empty"},
     {"element":"dial_code","error":"Value is required and can't be empty"},    
     {"element":"country_code","error":"Value is required and can't be empty"}
  ]
}

您需要将服务器代码更改为:

$response->setContent(json_encode(array("errors" => $errors)));

然后您可以使用这样的响应数据:

var response = data; // data is JSON from server
$.each(response.errors, function (i, field) {
  console.log(field); // alert(field);
});