我将json数据输入Sencha touch应用程序。这是json:
http://pastie.org/2622260 - (当然是为一个例子而修改)
当我返回“images”和console.log时,它返回以下内容:
images: "[object Object],[object Object],[object Object],[object Object],[object Object]"
而不是网址。
我的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();
我正在寻找的是返回图像网址数组,而不是我目前获得的[对象对象]。
编辑:这是我用来尝试拉出图像的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,
});
答案 0 :(得分:3)
编码为JSON的PHP中的关联数组在解码时成为JavaScript中的对象。你所看到的是正确和正常的。像在JavaScript中的任何其他对象一样访问它们。
答案 1 :(得分:1)
更新:对不起,对于我提供的不相关的例子
您的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);
}
答案 2 :(得分:0)
找到了答案,把它交给了Rifat,因为他的回答是跳板。
我让[对象对象]以字面字符串形式出现。我在Sencha touch之外写了一个快速的test.php,并且能够让实际的数组工作。我的第一个问题是json本身 - 需要进行验证。你们都是正确的,谢谢你的线索。
其次,一旦我将test.php文件工作,我意识到它必须在Sencha本身内。这串人物与它有关的唠叨感觉坐在我的脑海里。最后,我去了我的模型,发现我是如何拉动图像的:
{name: "images", type: "string"},
我看到了弦,aha时刻袭来!这修好了它并给了我我的网址:
{name: "images"},
不能完全感谢Ignacio和Rifat。你们和我一起住在那里,得到了(最终)出现的答案。再次感谢,并希望这可以帮助其他可能遇到类似问题的人。
答案 3 :(得分:0)
我在javascript后端曾经遇到过类似的问题。以下是使用JS的人的答案:
确保在发送对象数组之前对其进行字符串化:
JSON.stringify(array[i]);
除非您只发送JSON,在这种情况下发送常规JSON应该可以正常工作:)