admin管理员组文章数量:1399269
I using JavaScript JSON library to parse JSON encoded array, received via POST.
Here is my code:
var itemsRequest = '[{"id":"142"},{"id":"152"}]';
var items = JSON.parse(itemsRequest);
for(var i = 0; i<items.count(); i++)
{
var item = items[i];
alert(item.id);
}
I am not sure why, but the parser is just not liking that. How can I get it to parse?
I using JavaScript JSON library to parse JSON encoded array, received via POST.
Here is my code:
var itemsRequest = '[{"id":"142"},{"id":"152"}]';
var items = JSON.parse(itemsRequest);
for(var i = 0; i<items.count(); i++)
{
var item = items[i];
alert(item.id);
}
I am not sure why, but the parser is just not liking that. How can I get it to parse?
Share Improve this question asked Nov 18, 2011 at 0:44 user1052933user1052933 1912 gold badges4 silver badges8 bronze badges 1-
What does "not liking that" mean? Do you get an error message in the console? What actually happens? What do you get for
console.log(items)
? – Phrogz Commented Nov 18, 2011 at 0:46
3 Answers
Reset to default 4Try items.length
instead of items.count()
.
An array doesn't have a count
method. Use the length
property:
for (var i = 0; i < items.length; i++) {
Demo: http://jsfiddle/Guffa/Rt4db/
Below is the very good way to do:
var itemsRequest = '[{"id":"142"},{"id":"152"}]';
var items = eval(itemsRequest); //Converted to actual JSON data
for (var item in items) {
alert(items[item]['id']);
}
Hope this is very helpful, thanks
本文标签: Can not parse JSON with JavaScript parser Square bracketsStack Overflow
版权声明:本文标题:Can not parse JSON with JavaScript parser. Square brackets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744200462a2594937.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论