JSON返回时带有“返回”\&引用;(拉姆达)

JSON返回时带有“返回”\&引用;(拉姆达),json,amazon-web-services,http-get,aws-lambda,openweathermap,Json,Amazon Web Services,Http Get,Aws Lambda,Openweathermap,我使用AWS Lambda从开放天气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,

我使用AWS Lambda从开放天气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}"
 res.on('end', function() {

        result = body.replace('\\', '');
        context.succeed(result);
    });
这将停止我的over服务,使用(SwiftJSON)将其检测为有效的JSON

有人能告诉我如何使API信息以正确格式的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}"
 res.on('end', function() {

        result = body.replace('\\', '');
        context.succeed(result);
    });
它没有改变任何东西。仍然有相同的输出。

您可以使用:

    res.on('end', function() {
 context.succeed(body.replace(/\\/g, '') );

若要将\替换为空..

则将其作为字符串发布

尝试context.success(JSON.parse(result))

从文件中

提供的结果必须与JSON.stringify兼容。如果AWS Lambda未能stringify或遇到其他错误,将引发未处理的异常,并将X-Amz-Function-error响应头设置为unhandled


因此,本质上,它将json字符串作为字符串,并对其调用json.stringify…从而转义您看到的所有引号。传递解析后的json对象以获得成功,它不应该出现此问题。对于Java,只需返回JSONObject。返回字符串时,它会尝试进行一些转换,并最终导致错误为所有引号加上限。

对结果执行此操作:response.json()

如果使用Java,响应可以是用户定义的对象,如下所示

class Handler implements RequestHandler<SQSEvent, CustomObject> {
    public CustomObject handleRequest(SQSEvent event, Context context) {
        return new CustomObject();
    }
}
类处理程序实现RequestHandler{
公共CustomObject HandlerRequest(SQSEvent事件、上下文){
返回新的CustomObject();
}
}

可以找到示例代码。

我在哪里使用它?我更新了答案,如果它不起作用,请尝试json.parse(body)@JamesG我再次更新了代码,将此代码替换为res.on('end',function(){context.success(body);谢谢杰夫。非常感谢这似乎不能回答这个问题。请你详细解释一下这是如何解决这个问题的。