2010-08-01 36 views
0

當談到json時,我是一個noob。我越用它,我開始喜歡它。我有一個看起來像這樣的輸出從json輸出中檢索數據的問題

[ 
    { 
     "product": { 
      "id": "6", 
      "category": "Books", 
      "created": "2010-04-13 15:15:18", 
     }, 
     "availability": [ 
      { 
       "country": "United Kingdom", 
       "price": "$13.99", 
      }, 
      { 
       "country": "Germany", 
       "price": "$13.99", 
      } 
     ]    
    } 
] 

其實我只列出了一個產品,但輸出有許多列出的產品。我想循環瀏覽這個json輸出並獲取所有產品的類別,可用的國家和使用fbjs的價格。

我很感激任何幫助。

謝謝。

+2

你能告訴我們你使用遍歷解析JSON的代碼? – theycallmemorty 2010-08-01 13:33:27

+0

@theycallmemorty我想我會使用$ .each方法,但我不知道fbjs中的相同。 – 2010-08-01 13:51:31

回答

1

如果你的JSON是這樣的,

var products = [ 
    { 
     "product": { 
      "id": "6", 
      "category": "Books", 
      "created": "2010-04-13 15:15:18", 
     }, 
     "availability": [ 
      { 
       "country": "United Kingdom", 
       "price": "$13.99", 
      }, 
      { 
       "country": "Germany", 
       "price": "$13.99", 
      } 
     ]    
    }, 
    { 
     "product": { 
      "id": "7", 
      "category": "Books", 
      "created": "2010-04-13 15:15:18", 
     }, 
     "availability": [ 
      { 
       "country": "United Kingdom", 
       "price": "$13.99", 
      }, 
      { 
       "country": "Germany", 
       "price": "$13.99", 
      } 
     ]    
    } 
] 

您可以遍歷:

for(var index = 0; index < products.length; index++) { 
    alert(products[index].product.category); 
}