【问题标题】:JSON returned as individual string and not objectsJSON 作为单个字符串而不是对象返回
【发布时间】:2011-08-20 20:07:00
【问题描述】:

我正在使用 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)
}

它就像一个字符串。假设结果是“浪荡公子”,它将返回:
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 返回

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

【问题讨论】:

  • 还发布一些您收到的 json 响应
  • 当 netflix 已经返回 json 时,为什么还要执行 json_encode?我不熟悉 netflix api,但从我在你的问题中看到的。这对我来说似乎很奇怪。也许你应该只传递 netflix 响应,因为它已经输出为 json?
  • @Ivan Ivanić i json_encode 响应,因为如果我只是回显响应或只是返回它,那么我会收到一个 php 错误,即 Object 无法转换为字符串。将编辑问题以显示 json 返回

标签: php json codeigniter jquery


【解决方案1】:

in,正如您所注意到的,它不会按照您的意愿处理数组。使用$.each

【讨论】:

  • 我将代码发送到$.each(data.autocomplete.autocomplete_item, function(i) { response(data.autocomplete.autocomplete_item[i].title.short); }),我仍然得到相同的结果
  • 嗯,应该是$.each(data.autocomplete.autocomplete_item, function(i, item) { response(item.title.short); }),不过很明显,autocomplete_item 是一个字符串……
  • 没有骰子...它仍然循环遍历数组值作为字符串位置并返回字符串位置。文本框将有“Swingers”作为搜索,自动完成显示:S w i n g e r S very奇怪和令人沮丧的行为......
【解决方案2】:

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

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

【讨论】:

  • 没有骰子...循环在某处返回单个元素而不是位置。如在文本框中将是“Swingers”,但自动完成看起来像 S w i n g e r s
【解决方案3】:

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

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

所需要的是将一组对象发送回视图。谢谢大家的回答和帮助!!

【讨论】:

    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 2013-02-23
    • 2021-11-03
    • 2011-08-15
    • 2017-09-22
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    相关资源
    最近更新 更多