2011-10-01 7 views
1

다음 JSON 인코딩 데이터가 반환되며이를 jQuery와 함께 처리해야합니다.
jQuery.getJSON()의 콜백 기능을 사용하여이 목록에서 다른 저장소와 차량 데이터에 액세스하려면 어떻게해야합니까?jQuery를 사용하여 JSON 처리

$.getJSON('url', function(data) { 
    // ...? 
}); 

JSON 부호화 데이터 :

// top-level result is a list of dictionaries 
[ 
    // return dictionary for each depot 

    { 
    depot: { 
     _id: 'D3', 
     intersection: { 
     first: 'Bay', 
     second: 'King' 
     }, 
     address: { 
     number: '100', 
     street: 'King street West', 
     city: 'Toronto', 
     province: 'ON', 
     postal_code: 'M5X 1B8' 
     }, 
    }, 
    // return dictionary for each car in that depot 
    vehicle: [{ 
     _id: 'V4', 
     _depot_id: 'D3', 
     model: 'Ford F150', 
     price: '80', 
     km_per_litre: '15', 
     cargo_cu_m: 'YES', 
     category: 'Truck', 
     image: 'www.coolcarz.com' 
     }, { 
     _id: 'V24', 
     _depot_id: 'D3', 
     model: 'Toyota Camry Hybrid', 
     price: '90', 
     km_per_litre: '25', 
     cargo_cu_m: 'YES', 
     category: 'Hybrid car', 
     image: 'www.coolcarz.com' 
     } 
    ] 
    }, 


    { 
    depot: { 
     _id: 'D9', 
     intersection: { 
     first: 'Bay', 
     second: 'Front' 
     }, 
     address: { 
     number: '161', 
     street: 'Bay', 
     city: 'Toronto', 
     province: 'ON', 
     postal_code: 'M5J 2S1' 
     }, 
    }, 
    // return dictionary for each car in that depot 
    vehicle: [{ 
     _id: 'V11', 
     _depot_id: 'D9', 
     model: 'Ford Crown Victoria', 
     price: '45', 
     km_per_litre: '13', 
     cargo_cu_m: 'YES', 
     category: 'Standard car', 
     image: 'www.coolcarz.com' 
     }, 
    ] 
    }, 

] 

답변

2
$.getJSON('url', function(data) { 
    alert(data.length);      // 2 
    alert(data[0].depot.intersection.second); // "King" 
    alert(data[0].vehicle[1].category);  // "Hybrid car" 
    alert(data[1].depot.address.city);   // "Toronto" 
});