我是Node.JS的新手,我能够解析JSON数据并执行控制台日志以打印出名称和徽章。
var details = JSON.parse(body);
console.log(details.name, details.badges.length);
但我不知道如何获取bagdes数组中的数据,如id,name,url。
我试过
console.log(details.badges.length.id);
但没有出现。我该如何访问?谢谢。
{
"name": "Andrew Chalkley",
"badges": [
{
"id": 49,
"name": "Newbie",
"url": "http:\/\/teamtreehouse.com\/chalkers",
"icon_url": "https:\/\/achievement-images.teamtreehouse.com\/Generic_Newbie.png",
"earned_date": "2012-07-23T19:59:34.000Z",
"courses": [
]
},
{
"id": 26,
"name": "Introduction",
"url": "http:\/\/teamtreehouse.com\/library\/html\/introduction",
"icon_url": "https:\/\/achievement-images.teamtreehouse.com\/HTML_Basics.png",
"earned_date": "2012-07-23T21:57:24.000Z",
"courses": [
{
"title": "HTML",
"url": "http:\/\/teamtreehouse.com\/library\/html",
"badge_count": 1
},
{
"title": "Introduction",
"url": "http:\/\/teamtreehouse.com\/library\/html\/introduction",
"badge_count": 1
}
]
}
}
答案 0 :(得分:1)
这是一个数组,因此您需要索引,例如:details.badges[0].id
这将返回第一个(索引0)元素id。
.length
只返回数组的长度,因此获取数据没有用。