Accessing nested properties json

前端 未结 1 1746
谎友^
谎友^ 2021-01-27 19:36

I have this json:

{\"objects\":[{\"text\":{\"x\":643,\"y\":71,\"width\":82,\"height\":33,\"font\":\"Arial\",\"style\":\"bold\",\"size\":24,\"label\":\"Part A\"}}         


        
相关标签:
1条回答
  • 2021-01-27 20:18

    Use Object.keys(data) and access the first item. If you run the snippet you should see the types alert as expected:

    var data = {"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"}}]};
    
    
    $.each(data, function(index, firstLevel) {
      $.each(firstLevel, function(anotherindex, secondLevel) {
        alert(Object.keys(secondLevel)[0]);
        $.each(secondLevel, function(yetAnotherIndex, thirdLevel) {
          //alert(thirdLevel.y+''+thirdLevel.y);
        });
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
提交回复
热议问题