Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript에서 중첩된 json 객체에 액세스하는 방법은 무엇입니까?


중첩된 json 액세스 객체는 중첩 배열에 액세스하는 것과 같습니다. 중첩된 개체 다른 개체 안에 있는 개체입니다.

다음 예에서 'vehicles'는 'person'이라는 주요 객체 내부에 있는 객체입니다. 점 표기법 사용 중첩된 개체의 속성(car)에 액세스합니다.

예시-1

<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

예시-2

다음 예에서 "air-lines"라는 개체는 이중 중첩됩니다. (중첩된 객체 내부에 중첩됨). 이중 중첩 객체(lufthansa)의 속성은 점 표기법을 통해 액세스됩니다. 아래와 같습니다.

<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