如何解析jQuery的JSON阵列? [英] How to parse JSON array in jQuery?

查看:136
本文介绍了如何解析jQuery的JSON阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改我检查了jQuery的文档,并使用$阿贾克斯的JSON数据类型指定返回一个评估的JavaScript对象,这样的eval()是不是这里的答案。我知道,无论如何,因为我能够解析单一JSON对象,只是没有数组。问题是$。每个-ING通过他们我的方式:)

EDIT I checked the jQuery documentation and using $.ajax with the json datatype specified returns an evaluated javascript object, so eval() isn't the answer here. I knew that anyway, since I am able to parse single JSON objects, just not arrays. The problem is the $.each-ing my way through them :)

我按照解析jQuery的JSON数组的信的语法,但它不会出于某种原因。我通过获取$阿贾克斯阵,已经指定了正确的数据类型,并在Firebug可以看到,从我的PHP脚本的响应是[]。然而,当我尝试使用$。每次通过数组迭代,我得到的是不确定的值时,我尝试CONSOLE.LOG阵列的各个部分。这里是我的PHP脚本,使何恩codeS数组:

I have followed the syntax for parsing a JSON array in jQuery to the letter, but it doesn't work for some reason. I am fetching the array using $.ajax, have specified the correct datatype, and in Firebug can see that the response from my PHP script is []. Yet when I try to use $.each to iterate through the array, all I get is undefined values when I try to console.log various parts of the array. Here is where my php script makes and encodes the array:

if(mysqli_num_rows($new_res) > 0) {
$messages = array();

while($message_data = mysqli_fetch_assoc($query_res)) {
  $message = array(
    'poster' => $message_data['poster'],
    'message' => $message_data['message'],
    'time' => $message_data['time']
  );

  $messages[] = $message;
}

echo json_encode($messages);
} else if(mysqli_num_rows($new_res) == 0) {
$message = array(
  'poster' => '',
  'message' => 'No messages!',
  'time' => 1
);

echo json_encode($message);
}

和这里是我试图解析:

   var logged_in = '<?php echo $logged_in; ?>';
   var poster = '<?php echo $_SESSION["poster"];?>';
     $.ajax({
     url: 'do_chat.php5',
     type: 'post',
     data: ({'poster':poster,'logged_in':logged_in}),
     dataType: 'json',
     success: function(data) {
         $.each(data, function(messageIndex, message) {
                    console.log(parseInt($('#chatWindow :last-child > span').html())+' '+message['time']);
       if((parseInt(message['time']) > parseInt($('#chatWindow :last-child > span').html()))) {
     $('#chatWindow').append('<div class="poster">'+message['poster']+'</div><div class="message"><span>'+message['time']+'</span>'+message['message']+'</div>');
       }
       });
     }
     });

如果没有$。每个功能我能够成功地解析单个JSON对象,而不是一个数组。这是我第一次郊游JSON和$。每次,我pretty新的jQuery的,这样下去容易,如果我的code的丑位!

Without the $.each function I am able to successfully parse single JSON objects, but not an array. This is my first outing with JSON and $.each, and I'm pretty new to jQuery, so go easy if my code has ugly bits!

推荐答案

没有与评估是不是安全的,你可以使用JSON解析器是更安全: VAR myObject的= JSON.parse(数据);
对于这种用lib <一个href=\"https://github.com/douglascrockford/JSON-js\">https://github.com/douglascrockford/JSON-js

No, with eval is not safe, you can use JSON parser that is much safer: var myObject = JSON.parse(data); For this use the lib https://github.com/douglascrockford/JSON-js

这篇关于如何解析jQuery的JSON阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆