开发者

JSON returned as individual string and not objects

I am using Codeigniter and trying to use the jQuery autocomplete with it. I am also using @Phil Sturgeon client rest library for Codeigniter because I am getting the autocomplete data from netflix. I am return correct JSON and I can access the first element with

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

but when I loop through the results

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

it acts like a string. Lets say the result is "Swingers", it will return:

Object.value = s

Object.value = w

Object.value = i

and so on.

the 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);
        };

the controller:

public function search(){
    $search = $this->input->post('q');
    // Ru开发者_如何学Cn 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);
    }

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

any ideas? thanks.

there are some console logs with the return

the url


in, as you've noticed, doesn't do what you'd like with arrays. Use $.each


As far as I know, for (property in object) means that you want to access each of its properties rather than accessing them via the index. If you want to access them via the index, you probably want to use the standard for loop.

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

or if you still want to use your code, try this:

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

I haven't test them yet but I think you have the idea.


ok I figured out the correct format that I need to send to the autocomplete response method:
the view

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

the controller:

$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);

what was needed was to send back to the view an array of objects. Thank you guys for all your answers and help!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜