0

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?

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)? Commented Nov 18, 2011 at 0:46

3 Answers 3

4

Try items.length instead of items.count().

Sign up to request clarification or add additional context in comments.

Comments

3

An array doesn't have a count method. Use the length property:

for (var i = 0; i < items.length; i++) {

Demo: http://jsfiddle.net/Guffa/Rt4db/

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.