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

Javascriptがそれに役立つかもしれない関数フィルタを()持っていますが、それはありません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

関連する問題