2010-12-11 3 views
1

나는 쿼리해야하는 JSON 배열과 비슷한 javascript 객체를 가지고 있습니다. jQuery를 사용하여 전체 객체를 반복하지 않고 모든 노드를 순차적으로 테스트하여 노드 값을 찾는 방법이 있는지 알고 싶습니다. (하지 않고, 다시 중첩 된 모든 일을 통해 각()) ID를 부여하는 경우jQuery를 사용하여 JSON 객체 검색

Test = {}; 

Test.Species = { 
    "Category": [{ 
     "Product": [{ 
      "id": "a01", 
      "name": "Pine", 
      "description": "Short description of pine." 
     }, 
     { 
      "id": "a02", 
      "name": "Birch", 
      "description": "Short description of birch." 
     }, 
     { 
      "id": "a03", 
      "name": "Poplar", 
      "description": "Short description of poplar." 
     }], 
     "id": "A", 
     "title": "Cheap", 
     "description": "Short description of category A." 
    }, 
    { 
     "Product": [{ 
      "id": "b01", 
      "name": "Maple", 
      "description": "Short description of maple." 
     }, 
     { 
      "id": "b02", 
      "name": "Oak", 
      "description": "Short description of oak." 
     }, 
     { 
      "id": "b03", 
      "name": "Bamboo", 
      "description": "Short description of bamboo." 
     }], 
     "id": "B", 
     "title": "Moderate", 
     "description": "Short description of category B." 
    }, 
    { 
     "Product": [{ 
      "id": "c01", 
      "name": "Ebony", 
      "description": "Short description of ebony." 
     }, 
     { 
      "id": "c02", 
      "name": "Rosewood", 
      "description": "Short description of rosewood." 
     }, 
     { 
      "id": "c03", 
      "name": "Bubinga", 
      "description": "Short description of bubinga." 
     }], 
     "id": "C", 
     "title": "Expensive", 
     "description": "Short description of category C." 
    }] 
}; 


가 어떻게 제품의 이름과 설명을 검색 할 수 있습니다 : 예를 들어, 여기에 목적인가? jQuery의 find() 및 속성 선택자 ([id = a03])를 성공하지 못했습니다. 노드를 정확하게 타겟팅 할 수있는 유일한 방법은 인덱스 위치 (예 : var x = $(Test.Species.Category[2].Product[1].attr('description'));)를 사용하는 것이지만 목표가 아닙니다.

나는이 라인을 따라 뭔가 더 찾고 있어요,하지만이 작동하지 않습니다

var x = $(Test.Species.Category[id='B'].Product[id='b02'].attr('description'));

+0

자바 스크립트가 그와 함께 도움이 될 함수 필터()을 가지고 있지만 그것은 않습니다 IE7 이하에서는 작동하지 않습니다. – Pieter

답변

0
var x = $(Test.Species.Category[0].Product[id='b02'].attr('description')); 
var x = $(Test.Species.Category[0].Product[id='b02'].attr('name')); 
+0

작동하지 않음 (Chrome 8, IE8) – brnwdrng

관련 문제