我在NodeJS中獲取一些JSON字段時遇到問題。 這裏是我的JSON結構:在JSON中獲取特定字段
{
"header":
{
"file1":0,
"file2":1,
"subfiles":{
"subfile1":"true",
"subfile2":"true",
}
},
"response":
{
"number":678,
"start":0,
"docs":[
{
"id":"d3f3d",
"code":"l876s",
"country_name":"United States",
"city":"LA"
},
{
"id":"d2f2d",
"code":"2343g",
"country_name":"UK",
"city":"London"
}
]
}
}
如何訪問特定的字段(ID,城市或COUNTRY_NAME)在結構上也是這樣嗎? 我想在NodeJS中的東西,但我不能得到特定的領域。
request('http://url.com', function (error, res, body) {
if (!error && res.statusCode == 200) {
console.log(body)
}
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var urlResponse = JSON.parse(body);
console.log("Got a response: ", urlResponse.response.docs.city);
});
});
你有「文檔」的數組,所以你必須選擇一個。 – Joe