【问题标题】:Json Returning [object object] instead of arrayJson返回[object object]而不是数组
【发布时间】:2011-10-01 14:51:29
【问题描述】:

我将 json 数据输入到 Sencha 触摸应用程序中。这是json:

http://pastie.org/2622260 - (当然是修改为示例)

当我返回“图像”并 console.log 时,它会返回以下内容:

images: "[object Object],[object Object],[object Object],[object Object],[object Object]"

而不是 URL。

我的 json 编码器看起来像这样(我从 Wordpress 网站中提取数据):

 case 'posts':
    foreach(get_posts('numberposts=50&category=') as $post) {

            $count = 0;
            $imgurl = array();
            foreach( get_group('Photo', $post->ID) as $images){
                $count++;
                $imgurl[] = array(
                    'count'=>$count,
                    'imageurl'=>get('photo_image_file', $count, 1, false, $post->ID),
                );
            }

      $json['posts'][] = array(
        'id'=>$post->ID,
        'title'=>$post->post_title,
        'body'=>apply_filters('the_excerpt', $post->post_content),
        'date'=>$post->post_date,
        'user'=>get_userdata($post->post_author)->user_firstname,            
    'images'=>$imgurl
      );

    }
}

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

    echo json_encode($json);
    exit();

我正在寻找它做的是返回图像 url 的数组,而不是我目前得到的 [object object]。

编辑:这是我用来尝试提取 imageurl 的 sencha 代码:

    console.log(this.record.data);

    console.log(this.record.data.images);
    console.log(this.record.data.images.length);

    var numberOfPages = this.record.data.images.length;
    // Create pages for the carousel
    var pages = [];
    for (var i=0; i<numberOfPages; i++) {
        pages.push(new Ext.Component({
            id: 'page'+i,
            cls: 'page',
            tpl: '<tpl for=".">{imageurl}</tpl>',
            //html:'test',
        }));
    }

    // Create the carousel
    this.carousel = new Ext.Carousel({
        id: 'carousel',
        title:'Gallery',
        iconCls:'photos2',
        defaults: {
            cls: 'card'
        },
        items: pages,
    });

【问题讨论】:

  • 这是在IE的调试工具里吗?
  • 你是加入阵列还是什么?只有在将对象转换为字符串时才会得到这个。

标签: php javascript json sencha-touch


【解决方案1】:

PHP 中编码为 JSON 的关联数组在解码时会成为 JavaScript 中的对象。你看到的是正确和正常的。像访问 JavaScript 中的任何其他对象一样访问它们。

【讨论】:

  • 所以你的意思是我需要在煎茶触摸端解码它们?我不必对任何其他数据执行此操作,为什么要这样做?只是为了澄清它返回的 [object object] 字符串,而不是我可以看到 URL 的数组。如果我做一个 record.data.length 它会给我一个字符数而不是说,例如,数组中有 5 个图像。
  • 它不是返回一个“[object Object]”的字符串,它返回的是一个转换成字符串后看起来像这样的对象。
  • 所以我错过了一些东西。当请求它们出现在应用程序中时,我需要解码 json 对象吗?我不应该能够打开控制台并查看我的日志并查看对象值吗?
  • 1) 它们已经被解码。 2) 你为什么认为 JavaScript 是这样工作的?
  • 1.好的,2。说实话,无知。我想要做的是使用 sencha 将 imageurl 从对象中取出。我将使用我用来获取图像/图像 URL 的 sencha 代码更新我的问题。感谢您的帮助和理解
【解决方案2】:

更新:抱歉,我给出的示例无关紧要

你的pages[i].update(this.record.data.images);在哪里?

你可以试试下面的-

var numberOfPages = this.record.data.length;
// Create pages for the carousel
var pages = [];
for (var i=0; i<numberOfPages; i++) {
    var tmp = new Ext.Component({
        id: 'page'+i,
        cls: 'page',
        tpl: '<tpl for=".">{imageurl}</tpl>'
    });
    tmp.update(this.record.data[i].images);
    pages.push(tmp);
}

【讨论】:

  • 好主意,但有两个问题: 1. 页数返回字符数(一个例子是创建 75 张卡片,而不是图像的 5 张)。 2. this.record.data.images[i].imageurl 没有返回任何东西,只是一张空卡。也许我没有以某种方式触发 imageurl?
  • 能否请您发布正确的 JSON 输出。你上面的那个坏了:|
  • 我的 json 很长,但是这里有一个新的 Pastbin,只有一个帖子,我认为它是正确的:pastie.org/2622399
  • 为了回答您的更新问题,我的控制器使用以下方法将数据输入容器: var singlePost = App.views.postView.add({ xtype: 'postsingleview', record: options.record }) ;我使用默认功能提取数据:默认值:{ data:this.record.data }
  • 我认为这是正确的方向,但现在没有卡片出现,我认为 this.record.data.images 调用的数据不正确。 console.log(this.record.data.images);只产生[object Object],[object Object],[object Object],[object Object],[object Object]
【解决方案3】:

找到了答案,把它交给了 Rifat,因为他的答案就是跳板。

我将 [object object] 作为文字字符串传入。我在 Sencha touch 之外写了一个快速的 test.php 并且能够让实际的数组工作。我的第一个问题是 json 本身 - 它需要验证。你们都是正确的,谢谢你的线索。

其次,一旦我得到了 test.php 文件,我意识到它必须在 Sencha 本身中。那种认为这串字符与它有关的感觉在我脑海中萦绕。最后,我找到了我的模型,发现了我是如何拉取图像的:

{name: "images", type: "string"},

我看到了绳子,顿时惊呆了!这修复了它并给了我我的网址:

{name: "images"},

非常感谢 Ignacio 和 Rifat。你们和我一起呆在那里,得到了(最终)出现的答案。再次感谢,希望这对可能遇到类似问题的其他人有所帮助。

【讨论】:

    【解决方案4】:

    我曾经在 javascript 后端遇到过类似的问题。这是给使用 JS 的人的答案:

    确保在发送对象数组之前对其进行字符串化:

    JSON.stringify(array[i]);
    

    当然,除非您只发送 JSON,在这种情况下发送常规 JSON 应该可以正常工作:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 2019-07-16
      • 2019-10-18
      • 2021-01-29
      • 2016-03-08
      相关资源
      最近更新 更多