2010-08-01 59 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); 
}