解析node.js中的JSON
如何解析node.js中的以下JSON以提取temp和city的值解析node.js中的JSON,json,node.js,parsing,Json,Node.js,Parsing,如何解析node.js中的以下JSON以提取temp和city的值 { "message":"", "cod":"200", "type":"base", "calctime":"", "units":"internal", "count":1, "list": [ {"id":2823368, "coord":{"lat":47.666672,"lon":9.6}, "name":"London", "main":{"te
{
"message":"",
"cod":"200",
"type":"base",
"calctime":"",
"units":"internal",
"count":1,
"list":
[
{"id":2823368,
"coord":{"lat":47.666672,"lon":9.6},
"name":"London",
"main":{"temp":275.79,"pressure":1020,"humidity":74,"temp_min":272.59,"temp_max":281.48},
"dt":1362137169,
"date":"2013-03-01 11:26:09",
"wind":{"speed":1.5,"deg":0},
"clouds":{"all":90},
"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
"sys":{"country":"DE","population":18135},
"url":"http:\/\/openweathermap.org\/city\/2823368"
}
]
}
我通过以下方式获得上述JSON:
var response = JSON.parse(body);
console.log(response);
任何帮助都将不胜感激
var temp = response.list[0].main.temp;
var city = response.list[0].name;
至于“city”,我不确定您在寻找什么,因为您的输入中没有该名称的键,但我猜了一下。使用下面的内容获取临时和城市(来自url的城市),或者使用名称
var temp = response.list[0].main.temp,
url = response.list[0].url,
city = url.split('/')[3],
name = response.list[0].name;