Json返回[object object]而不是array


Json Returning [object object] instead of array

我有json数据被馈送到Sencha touch应用程序。

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

当我返回"images"和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]。

编辑:这是我用来尝试拉出图像的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,
    });

PHP中以JSON编码的关联数组在解码后成为JavaScript中的对象。你看到的是正确和正常的。

更新:对不起,对于我给出的不相关的示例

你的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);
}

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

我把[object object]作为一个字符串来处理。我在Sencha touch之外编写了一个快速的test.php,并能够使实际的数组工作。我的第一个问题是json本身——它需要被验证。你们都是对的,谢谢你们的提示。

第二,一旦我让test.php文档工作,我意识到它必须在Sencha本身。我脑子里萦绕着一种挥之不去的感觉,觉得这串字符与它有关。最后,我去了我的模型,发现我是如何提取图像的:

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

我看到了字符串,灵光一现!这修复了它,并给了我我的URL的:

{name: "images"},

非常感谢Ignacio和Rifat。你们和我一起坚持了下来,得到了最终出现的答案。再次感谢,并希望这有助于其他人可能会遇到类似的问题。

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

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

JSON.stringify(array[i]);

除非你只发送JSON,在这种情况下发送常规JSON应该工作得很好:)