访问json的属性

时间:2014-12-27 00:59:26

标签: json node.js

我有这个JSON输出

  {  
   "status":1,
   "complete":1,
   "list":{  
      "792489954":{  
         "item_id":"792489954",
         "resolved_id":"792489954",
         "given_url":"http:\/\/www.liveathos.com\/?gclid=Cj0KEQiAzb-kBRDe49qh9s75m-wBEiQATOxgwZcJ5_ws34o4PUSUYDGqs8HEbLF-LyjxrTPOwn6AYV8aAmMk8P8HAQ",
         "given_title":"Athos - Wearable Technology for Fitness",
         "favorite":"0",
         "status":"0",
         "time_added":"1418754744",
         "time_updated":"1418754746",
         "time_read":"0",
         "time_favorited":"0",
         "sort_id":0,
         "resolved_title":"Wearable Technology for Fitness",
         "resolved_url":"http:\/\/www.liveathos.com\/?gclid=Cj0KEQiAzb-kBRDe49qh9s75m-wBEiQATOxgwZcJ5_ws34o4PUSUYDGqs8HEbLF-LyjxrTPOwn6AYV8aAmMk8P8HAQ",
         "excerpt":"Thank you for reserving Athos. You will receive a confirmation email with reservation details and a referral link where you get $10 off your next order.",
         "is_article":"0",
         "is_index":"0",
         "has_video":"0",
         "has_image":"0",
         "word_count":"25"
      },
      "692647226":{  
         "item_id":"692647226",
         "resolved_id":"692647226",
         "given_url":"http:\/\/www.terrafugia.com\/news",
         "given_title":"News | Terrafugia",
         "favorite":"0",
         "status":"0",
         "time_added":"1418754204",
         "time_updated":"1418754204",
         "time_read":"0",
         "time_favorited":"0",
         "sort_id":1,
         "resolved_title":"News",
         "resolved_url":"http:\/\/www.terrafugia.com\/news",
         "excerpt":"",
         "is_article":"0",
         "is_index":"1",
         "has_video":"0",
         "has_image":"0",
         "word_count":"0"
      },
      `...etc`      "since":1419641101
   };

这是一个带有列表的JSON,其上应包含文章项。我想访问这些较小的项目的属性,如given_url。

我正在使用

for key in (BIGJSONRESPONSE).list
do etc...

当我尝试打印密钥时,我只获得项目之前的ID。知道怎么访问更多?

谢谢!

2 个答案:

答案 0 :(得分:3)

你问了钥匙,你拿到了钥匙。要获取与该键相关联的项目,请使用BIGJSONRESPONSE.list[key]

答案 1 :(得分:1)

这应该是正确的:

for in返回对象的键。由于密钥引用了对象的属性,因此可以使用[key]访问该属性。

for (key in (BIGJSONRESPONSE).list)
{
    console.log(BIGJSONRESPONSE.list[key]) // log the entry to console for debugger.
}