JSON作为单独的字符串而不是对象返回


JSON returned as individual string and not objects

我正在使用Codeigniter并尝试使用jQuery自动完成。我还使用@Phil Sturgeon客户端休息库Codeigniter,因为我从netflix获得自动完成数据。我返回正确的JSON,我可以访问第一个元素

response(data.autocomplete.autocomplete_item[0].title.short);

但是当我循环结果

for (var i in data.autocomplete.autocomplete_item) {
 response(data.autocomplete.autocomplete_item[i].title.short)
}

就像字符串一样。假设结果是"Swingers",它将返回:
对象。Value = s
对象。Value = w
对象。Value = i

等等

js:

$("#movies").autocomplete({
            source: function(request, response) {
          $.ajax({
             url: "<?php echo site_url();?>/welcome/search",
             dataType: "JSON",
             type:"POST",
             data: {
                q: request.term
             },
             success: function(data) { 
                for (var i in data.autocomplete.autocomplete_item) {
                 response(data.autocomplete.autocomplete_item[i].title.short);
                }
             }
          });
       }        
        }).data("autocomplete")._renderItem = function(ul, item) {
            //console.log(item);
        $(ul).attr('id', 'search-autocomplete');
           return $("<li class='""+item.type+"'"></li>").data( "item.autocomplete", item ).append("<a href='""+item.url+"'">"+item.title+"</a>").appendTo(ul);
        };

控制器:

public function search(){
    $search = $this->input->post('q');
    // Run some setup
    $this->rest->initialize(array('server' => 'http://api.netflix.com/'));
    // set var equal to results
    $netflix_query = $this->rest->get('catalog/titles/autocomplete', array('oauth_consumer_key'=>$this->consumer_key,'term'=> $search,'output'=>'json'));
    //$this->rest->debug();
    //$json_data = $this->load->view('nice',$data,true);
        //return $json_data;
        echo json_encode($netflix_query);
    }

json返回

{"autocomplete":
    {"autocomplete_item":[
       {"title":{"short":"The Strawberry Shortcake Movie: Sky's the Limit"}},
       {"title":{"short":"Futurama the Movie: Bender's Big Score"}},
       {"title":{"short":"Daffy Duck's Movie: Fantastic Island"}}
       ...

任何想法?谢谢。

有一些控制台日志返回
url

in,正如您所注意到的,不像您希望的那样处理数组。使用$.each

据我所知,for (property in object)意味着你想要访问它的每个属性,而不是通过索引访问它们。如果您想通过索引访问它们,您可能希望使用标准的for循环。

for (i = 0; i <= 10; i++) {
    response(data.autocomplete.autocomplete_item[i].title.short);
}

或者如果你还想使用你的代码,试试这个:

for (i in data.autocomplete.autocomplete_item) {
    response(i.title.short);
}

我还没有测试过,但是我想你们已经知道了。

好了,我找到了我需要发送给自动完成响应方法的正确格式:
查看

$("#movies").autocomplete({
        minLength: 2,
        source: function(request, response) {
            $.post("<?php echo base_url();?>welcome/search", {q: request.term}, 
               function(data){
                    //console.log(data);
                    response(data);
            }, 'json');
   }        
    });

控制器:

$search = $this->input->post('q');
        // Run some setup
        $this->rest->initialize(array('server' => 'http://api.netflix.com/'));
        // Pull in an array
        $netflix_query = $this->rest->get('catalog/titles/autocomplete', array('oauth_consumer_key'=>$this->consumer_key,'term'=> $search,'output'=>'json'),'json');
        $json = array();
        foreach($netflix_query->autocomplete->autocomplete_item as $item){
            $temp = array("label" => $item->title->short);
            array_push($json,$temp);
        }
        echo json_encode($json);

所需要的是返回给视图一个对象数组。谢谢大家的回答和帮助!!