1

Here is my object that I want to parse through:

my.test = [{"Customer":
    {
        "id":"123",
        "Name":"john"
    }
}]

I have this already:

$.each(Cmy.test, function(index, value) {
    $.each(value.Customer, function(innerIndex, innerValue) {
        alert('File ' + innerValue.id + ' in customer ' + index);
    });
});

But my alert() shows undefined for value. What am I doing wrong?

2
  • 3
    Your JSON looks a little incomplete it seems its missing a }] at the end Commented Apr 20, 2011 at 0:12
  • 1
    Surely "$.each(Cmy.test..." is a typo and is actually "$.each(my.test..."? Commented Apr 20, 2011 at 0:12

2 Answers 2

4
my.test = [{"Customer":
{
"id":"123",
"Name":"john",
}

should be

my.test = [{"Customer": {
  "id":"123",
  "Name":"john"
}}];

then iterate like this:

$.each(my.test, function(index) {
  alert('File ' + my.test[index].Customer.id + ' in customer ' + index);
});

At least that's what I think you're looking for.

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

2 Comments

Check brace count on object literal.
Also, trailing comma of death is lurking...
2

See jQuery.each -- when iterating over "non-array" objects the callback is passed (key, value).

For instance, in the above example the callback may be passed a key of "id" and a value of "123". As a result, "123".id (innerValue.id) is most likely nonsensical as it will evaluate to undefined.

The outer-loop is okay because (index, value) is passed in the callback for arrays. In this case the value is {Customer: ...}.

Happy coding.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.