Tuesday, September 29, 2009

[jQuery] loop through multi dimensional json?

Hi,

im sure this is a simple matter, but i cant seem to work out how to
loop through a multi dimensional JSON object (where the keys are
unknown).

example json:

{
"processed":false,
"message":"External link could not be set:",
"message_array":
"{
\"link\":
{
\"isEmpty\":\"Value is required and
can't be empty\"
}
}"
}

basically, i need to loop through the 'message_array' key and put each
error into an LI item.

doing this:

$.each(r, function( intIndex, objValue ) {
if (intIndex == 'message_array') {
html += "<li>" + intIndex + ": " + objValue+ "</li>";
}
});

puts the message_array bit into objValue, but thats where im stuck.
doing another each on that bit:

$.each(objValue , function( field, errors) {
html += "<li>" + field+ ": " + errors+ "</li>";
});

results in a load of LI items like so:

0: {
1: "
2: l
3: i
4: n
5: k
6: "
7: :
8: {
9: "
10: i
11: s
12: E:

.....
etc


any help appreciated.

No comments: