I have this json:
{"objects":[{"text":{"x":643,"y":71,"width":82,"height":33,"font":"Arial","style":"bold","size":24,"label":"Part A"}},
{"text":{"x":643,"y":116,"width":389,"height":42,"font":"Arial","style":"bold","size":16,"label":"What does \"novel\" mean as it is used in paragraph 8 of \"Turning Down a New Road\"? "}},
{"radiobutton":{"x":643,"y":170,"width":100,"height":20,"label":"A. old"}},{"radiobutton":{"x":643,"y":209,"width":100,"height":20,"label":"B. afraid"}},
{"radiobutton":{"x":643,"y":250,"width":100,"height":20,"label":"C. new"}},
{"radiobutton":{"x":643,"y":289,"width":100,"height":20,"label":"D. friendly"}}]}
I need to get the properties of each element, but I can't get the property of second level, I mean I can´t know if the element is a "text","radiobutton","label", I have no problem with the propeties of third level.
This is my source:
$.ajax({
url: 'generateobject.php',
dataType: 'json',
type: 'GET'
}).done(function(data) {
$.each(data, function(index, firstLevel) {
$.each(firstLevel, function(anotherindex, secondLevel) {
alert(secondLevel[0]);//shows [object Object]
$.each(secondLevel, function(yetAnotherIndex, thirdLevel) {
//alert(thirdLevel.y+''+thirdLevel.y);
});
});
});
});
How do I get the property of second level?
Use
Object.keys(data)
and access the first item. If you run the snippet you should see the types alert as expected: