简体   繁体   English

使用jQuery遍历JSON数据

[英]Traverse JSON data with jQuery

I can't figure out how to get a bunch of MySQL rows into a JSON data structure and iterate each of the row fields in java script. 我无法弄清楚如何将一堆MySQL行放入JSON数据结构中并迭代Java脚本中的每个行字段。

Here is my query in codeigniter 这是我在codeigniter中的查询

function get_search_results() {

    //$this->db->like('title', $searchText);
    //$this->db->orderby('title');
    $query = $this->db->get('movies');

    if($query->num_rows() > 0) {

        foreach($query->result() as $movie) {

            $movies[] = $movie->title;                

        }

    }

    return $movies;

}

Encode array for json 编码JSON数组

$rows= $this->movie_model->get_search_results();
echo json_encode($rows);

My jQuery AJAX request, 我的jQuery AJAX请求,

 $.ajax({
      type: "GET",
      url: "publishlinks/search_movies",
      data: searchString,
      ...

This is how I've been trying to traverse rows in the java script. 这就是我一直试图遍历Java脚本中的行的方式。 It is iterating over every character: 1 t 2 h 3 e 4 g ... 7 e I need this: 1 the game 2 lost 3 you 它遍历每个角色:1 t 2 h 3 e 4 g ... 7 e我需要这个:1游戏2输了3您

   success: 
      function(result) {                                
        $.each(result, function(key, val) {
        alert(key + ' ' + val);
      })

//alert(result);
}

It looks like it is treating the result as a string instead of parsed JSON, hence iterating over it as if it was a string. 看起来好像将结果视为字符串而不是经过解析的JSON,因此像对待字符串一样对其进行迭代。 This could mean that it isn't returning a clean JSON encoded string in the response, so I'd check the response to make sure it is valid JSON. 这可能意味着它没有在响应中返回干净的JSON编码的字符串,因此我将检查响应以确保它是有效的JSON。 jQuery is supposed to intelligently guess the content type and parse accordingly. jQuery应该能够智能地猜测内容类型并相应地进行解析。

You could also try the dataType: "json" option on the ajax request to force it to be parsed as JSON instead of letting jQuery guess. 您也可以在ajax请求上尝试使用dataType:“ json”选项,以将其强制解析为JSON,而不是让jQuery猜测。

Use the dataType property of $.ajax $.ajax documentation 使用$ .ajax $ .ajax文档的dataType属性

dataType 数据类型

"The type of data that you're expecting back from the server." “期望从服务器返回的数据类型。”

Use firebug to find the type of the response variable. 使用Firebug查找响应变量的类型。 In your case the response is string but it should be Array (or Object?) 在您的情况下,响应为字符串,但应为数组(或对象?)

If you don't want jQuery to automatically eval the json then live it as it is and in the success function add the following: 如果您不希望jQuery自动评估json,请直接使用json,并在成功函数中添加以下内容:

var parsed = $.parseJson(response);

Here is the documentation of the parseJson method: parseJson 这是parseJson方法的文档: parseJson

I think you problem is you don't fetch actual json but simple string response.... 我认为您的问题是您没有获取实际的json,而是简单的字符串响应。

use $.getJSON or specify dataType as json in your jquery ajax request! 使用$ .getJSON或在您的jquery ajax请求中将dataType指定为json!

Do you return a JSON header with your PHP ? 您是否使用PHP返回JSON标头?

header('Content-type: application/json');

Else try 其他尝试

result = JSON.decode(result);

before your "each" loop 在“每个”循环之前

Be sure to return proper content-type from your server side. 确保从服务器端返回正确的内容类型。 For JSON that would be application/json. 对于JSON,将是application / json。

$rows = $this->movie_model->get_search_results();
header('Content-type: application/json');
echo json_encode($rows);

Also add dataType: "json" to your jQuery request, just as beefsack said: 还要将dataType:“ json”添加到您的jQuery请求中,就像Beefsack所说的那样:

$.ajax({
  type: "GET",
  url: "publishlinks/search_movies",
  data: searchString,
  dataType: "json",
  ...
dataType: "json",
success:function(data){
var obj=$.parseJSON(data);
data=obj.data;
$.each(data,function(){
   //json data
})
}

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

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