简体   繁体   English

Json返回[对象对象]而不是数组

[英]Json Returning [object object] instead of array

I have json data being fed into a Sencha touch app. 我将json数据输入到Sencha touch应用中。 Here's the json: 这是json:

http://pastie.org/2622260 - (modified for an example, of course) http://pastie.org/2622260- (当然,以示例为例进行了修改)

When I return the "images" and console.log it, it returns the following: 当我返回“图像”并进行console.log记录时,它返回以下内容:

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

Rather than the URLs. 而不是URL。

My json encoder looks like this (I'm pulling the data out of a Wordpress site): 我的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();

What I am looking for it to do is to return the array of image urls, rather than the [object object] that I am currently getting. 我正在寻找要执行的操作,是返回图像URL的数组,而不是返回当前获得的[object object]。

EDIT: Here is the sencha code I'm using to try to pull the imageurl out: 编辑:这是我用来尝试拉出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,
    });

Associative arrays in PHP encoded as JSON become objects in JavaScript when decoded. 解码后,PHP中编码为JSON的关联数组成为JavaScript中的对象。 What you're seeing is correct and normal. 您看到的是正确和正常的。 Access them as you would any other object in JavaScript. 就像访问JavaScript中的其他任何对象一样访问它们。

Update: Sorry, for the irrelevant example I had given 更新:对不起,我给出的无关紧要的例子

Where is your pages[i].update(this.record.data.images); 您的pages[i].update(this.record.data.images);在哪里pages[i].update(this.record.data.images); ?

You can try the following - 您可以尝试以下方法-

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

I had a similar issue once, in the javascript backend. 我曾经在javascript后端遇到过类似的问题。 Here is an answer for people using JS: 这是使用JS的人的答案:

Make sure to stringify your object arrays before sending them: 在发送对象数组之前,请确保对其进行字符串化:

JSON.stringify(array[i]);

Unless ofcourse you are ONLY sending JSON, which in that case sending regular JSON should work fine :) 除非您当然只发送JSON,在这种情况下,发送常规JSON应该可以正常工作:)

Found the answer, gave it to Rifat because his answer was the springboard. 找到了答案,将其交给了Rifat,因为他的答案是跳板。

I had the [object object]'s coming through as a literal string of characters. 我有[object object]作为文字字符串来传递。 I wrote a quick test.php outside Sencha touch and was able to get the actual array to work. 我在Sencha touch之外编写了一个快速的test.php,能够使实际的数组工作。 My first issue was the json itself - It needed to be validated. 我的第一个问题是json本身-需要进行验证。 You guys were all correct and thank you for that clue. 你们都是正确的,并感谢您提供的线索。

Second, once I got the test.php document working, I realized it had to be within Sencha itself. 其次,一旦test.php文档开始工作,我就意识到它必须位于Sencha中。 The nagging feeling that the string of characters had something to do with it sat in my head. 一连串的烦恼让我感到of恼,这串字符与它有关。 Finally, I went to my model and found how I was pulling the images: 最后,我进入模型,发现如何提取图像:

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

I saw string, and the aha moment hit! 我看到绳子了,啊哈的一声打中! This fixed it and gave me my URL's: 这样就解决了,并给了我我的URL:

{name: "images"},

Can't thank Ignacio and Rifat enough. 非常感谢Ignacio和Rifat。 You guys hung in there with me and got the answer to (eventually) appear. 你们和我一起呆在那里,并得到了答案(最终)出现。 Thanks again, and hope this helps anyone else that may run into a similar issue. 再次感谢您,希望对其他可能遇到类似问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM