访问嵌套的json 对象就像访问嵌套的数组一样。嵌套对象是另一个对象内部的对象。
在以下示例中,“车辆”是位于称为“人”的主要对象内部的对象。使用点表示法访问嵌套对象的属性(汽车)。
<html> <body> <script> var person = { "name":"Ram", "age":27, "vehicles": { "car":"limousine", "bike":"ktm-duke", "plane":"lufthansa" } } document.write("Mr Ram has a car called" + " " + person.vehicles.car); </script> </body> </html>
输出结果
Mr Ram has a car called limousine
在下面的示例中,一个名为“ air-lines”的对象被双重嵌套(嵌套在嵌套对象内部)。该双嵌套对象(汉莎航空)的属性通过点表示法访问,如下所示。
<html> <body> <script> var person = { "name":"Ram", "age":27, "vehicles": { "car":"limousine", "bike":"ktm-duke", "airlines":{ "lufthansa" : "Air123", "British airways" : "Brt707" } } } document.write("Mr Ram travels by plane called" + " " + person.vehicles.airlines.lufthanza); </script> </body> </html>
输出结果
Mr Ram travels by plane called Air123