AWS Lambda を使用して、open weather api から JSON を取得して返します。
これが私のコードです:
var http = require('http');
exports.handler = function(event, context) {
var url = "http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a";
http.get(url, function(res) {
// Continuously update stream with data
var body = '';
res.on('data', function(d) {
body += d;
});
res.on('end', function() {
context.succeed(body);
});
res.on('error', function(e) {
context.fail("Got error: " + e.message);
});
});
}
それは機能し、JSON を返しますが、次のようにすべての"の前にバックスラッシュを追加しています。
"{\"coord\":{\"lon\":145.77,\"lat\":-16.92},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":303.15,\"pressure\":1008,\"humidity\":74,\"temp_min\":303.15,\"temp_max\":303.15},\"wind\":{\"speed\":3.1,\"deg\":320},\"clouds\":{\"all\":75},\"dt\":1458518400,\"sys\":{\"type\":1,\"id\":8166,\"message\":0.0025,\"country\":\"AU\",\"sunrise\":1458505258,\"sunset\":1458548812},\"id\":2172797,\"name\":\"Cairns\",\"cod\":200}"
これは、これを有効な JSON として検出する (SwiftJSON) を使用して、オーバー サービスを停止しています。
API 情報を正しくフォーマットされた JSON として出力する方法を誰か教えてもらえますか?
.replace
私は次のように試しました:
res.on('end', function() {
result = body.replace('\\', '');
context.succeed(result);
});
何も変わりませんでした。それでも同じ出力がありました。