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应该工作得很好:)
- 警告:失败的propType:类型为“array”的无效prop应为React的“object”
- 如何从〔object HTMLUListElement〕将UL从cookie获取到Array
- Array.insert函数错误,因为Object函数Array(){〔本机代码〕}没有方法'插入'
- TypeError:Object[Object Array]没有方法'getProperty'
- JavaScript array.push (object) with Angular ng-repeat 无法按预期运
- jQuery的返回值是Array,但构造函数是Object.如何
- CRM 2011 : Object [object Array] 没有方法 'getValue'
- Javascript 将带有子对象的 Object 转换为 Array
- JSON object to Javascript Array
- array.sort() 是否就地,即使在 Object.keys() 上操作也是如此
- 将本机数组/对象转换为 Ember.Array/Ember.Object 的最佳实践
- 为什么 Object.keys(Array.prototype) 返回空数组
- Google图表列图表给出Object〔Object Array〕没有方法'getColumnType'
- PHP将Object Array的一个字段复制到一个简单的Array中
- 美元.$里面的每个.append (Object [Object Array]没有方法'apply')
- Object|Array.observe() implementation for ECMA 5
- 当push到object/array时,ngShow不会更新
- js模板的输出是[object Array]而不是渲染的模板
- 如何在不使用原型的情况下获得Object/Array.prototype的功能
- Javascript Object Array to Json