考虑以下JSON对象数组:

myList = [
    {title:"Parent1",
        children:[{childname:"Child11"},
                     {childname:"Child12"}],
        cars:[{carname:"Car11"},
              {carname:"Car12"}]
    },
    {title:"Parent2",
        children:[{childname:"Child21"},
                     {childname:"Child22"}],
        cars:[{carname:"Car21"},
              {carname:"Car22"}]
    }
];

如何使用javascript访问“Child21”?以下选项不起作用:
var myString = myList[1].children[0].childname; //Does not work
var myString = myList[1]["children"][0].childname; //Does not work

最佳答案

这对我来说行得通:

myList[1].children[0].childname

这也可以:
myList[1]["children"][0].childname;

在全,
<html>
<body>
<script>
var myList = [
    {title:"Parent1",
        children:[{childname:"Child11"},
                     {childname:"Child12"}],
        cars:[{carname:"Car11"},
              {carname:"Car12"}]
    },
    {title:"Parent2",
        children:[{childname:"Child21"},
                     {childname:"Child22"}],
        cars:[{carname:"Car21"},
              {carname:"Car22"}]
    }
];
alert (myList[1].children[0].childname);
</script>
</body>
</html>

09-20 14:56