Php 访问嵌套属性json
我有一个json:Php 访问嵌套属性json,php,jquery,json,nested,Php,Jquery,Json,Nested,我有一个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\" mea
{"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"}}]}
我需要得到每个元素的属性,但是我不能得到第二级的属性,我的意思是我不知道元素是否是“文本”、“radiobutton”、“label”,我对第三级的属性没有问题
这是我的消息来源:
$.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);
});
});
});
});
如何获取第二级属性?使用对象.键(数据)
并访问第一项。如果运行代码段,您应该会看到预期的类型警报:
var data={“objects”:[{“text”:{“x”:643,“y”:71,“width”:82,“height”:33,“font”:“Arial”,“style”:“bold”,“size”:24,“label”:“Part A”},
{“文本”:{“x”:643,“y”:116,“宽度”:389,“高度”:42,“字体”:“Arial”,“风格”:“粗体”,“大小”:16,“标签”:“小说”在“开辟一条新路”第8段中的意思是什么?”,
{“radiobutton”:{“x”:643,“y”:170,“宽度”:100,“高度”:20,“标签”:“A.旧”},{“radiobutton”:{“x”:643,“y”:209,“宽度”:100,“高度”:20,“标签”:“B.害怕”},
{“单选按钮”:{“x”:643,“y”:250,“宽度”:100,“高度”:20,“标签”:“C.新”},
{“单选按钮”:{“x”:643,“y”:289,“宽度”:100,“高度”:20,“标签”:“D.友好”}};
$.each(数据,函数(索引,第一级){
$。每个(第一级,函数(另一级,第二级){
警报(对象键(第二级)[0]);
$.each(第二级,函数(yetAnotherIndex,第三级){
//警报(第三级y+“”+第三级y);
});
});
});代码>
这会有所帮助