【问题标题】:Parsing JSON in node.js在 node.js 中解析 JSON
【发布时间】:2013-02-15 22:54:57
【问题描述】:

我将如何解析 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":{"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);

任何帮助将不胜感激。

【问题讨论】:

    标签: json node.js parsing


    【解决方案1】:

    使用以下获取临时和城市(来自 url 的城市)或使用名称

    var temp = response.list[0].main.temp,
    url = response.list[0].url,
    city = url.split('/')[3],
    name = response.list[0].name;
    

    【讨论】:

      【解决方案2】:
      var temp = response.list[0].main.temp;
      var city = response.list[0].name;
      

      至于“城市”,我不确定您在寻找什么,因为您的输入中没有该名称的键,但我猜了一下。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-06
        • 2011-09-23
        • 2015-01-07
        • 2011-05-28
        • 2020-12-15
        • 1970-01-01
        • 1970-01-01
        • 2015-06-28
        相关资源
        最近更新 更多