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

时间:2011-08-20 20:07:01

标签: php json codeigniter jquery

我正在使用Codeigniter并尝试使用jQuery自动完成功能。我也在为Codeigniter使用@Phil Sturgeon客户端休息库,因为我从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”,它会返回:
Object.value = s
Object.value = w
Object.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 return

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

任何想法? 感谢。

有一些控制台日志与返回
the url

3 个答案:

答案 0 :(得分:0)

正如您所注意到的那样,

in不能用数组做你想做的事。使用$.each

答案 1 :(得分:0)

据我所知,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);
}

我还没有测试过,但我认为你有这个想法。

答案 2 :(得分:0)

好吧我想出了我需要发送到自动完成响应方法的正确格式:
视图

$("#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);

需要的是将一组对象发送回视图。谢谢你们所有的答案和帮助!!

相关问题