在 Step Function 中将 Json 字符串传递给 AWS Lambda - JsonReaderException 错误
Pass Json string to AWS Lambda in Step Function - JsonReaderException error
我正在尝试在 Step Function 中使用 AWS Lambda 函数。 Lambda 函数在单独测试并且 json 输入被转义时可以正常工作。但是,当通过步骤函数将输入传递给 lambda 函数时,出现 JsonReaderException 错误。我究竟做错了什么?社区是否知道此问题的解决方法?
lambda 函数:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Formatters.Binary;
using Amazon.Lambda.Core;
using Newtonsoft.Json.Linq;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AWSLambda1
{
public class Function
{
public void PostsBasedOnOddOrEven(string input, ILambdaContext context)
{
var details = JObject.Parse(input);
var postId = (int) details["id"];
var oddOrEvenResult = (int) details["OddOrEvenPostsResult"];
}
}
}
从 Step Function 到 Lambda 函数的输入:
{
"id": "1",
"OddOrEvenPostsResult": 2
}
Lambda 函数的输入(通过 Visual Studio 调用工作):
"{ \"id\": \"1\", \"OddOrEvenPostsResult\": 2}"
异常堆栈跟踪:
{
"errorType": "JsonReaderException",
"errorMessage": "Unexpected character encountered while parsing value: {. Path '', line 1, position 1.",
"stackTrace": [
"at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
"at Newtonsoft.Json.JsonTextReader.ReadAsString()",
"at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)",
"at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
"at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)",
"at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
"at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream requestStream)",
"at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
]
}
作为 Step Function 的一部分的 Lambda 函数不工作
Lambda 函数在单独测试时有效
由于您的 lambda 函数期望 input
成为 string
,框架会尝试将输入解析为 string
。
然而,输入实际上是一个JSON对象,而不是字符串。
因此解析器将失败并出现 "unexpected character" 错误。解析器需要一个 "
字符来指示字符串的开头。
那么,您可以按照以下方法解决它:
声明一个代表输入的c#class
public class FunctionInput
{
public int id { get; set; }
public int OddOrEvenPostsResult { get; set; }
}
更改函数签名以期望 input
类型为 FunctionInput
public class Function
{
public void PostsBasedOnOddOrEven(FunctionInput input, ILambdaContext context)
{
var postId = input.id;
var oddOrEvenResult = input.OddOrEvenPostsResult;
}
}
注意:您不需要自己解析输入。
我正在尝试在 Step Function 中使用 AWS Lambda 函数。 Lambda 函数在单独测试并且 json 输入被转义时可以正常工作。但是,当通过步骤函数将输入传递给 lambda 函数时,出现 JsonReaderException 错误。我究竟做错了什么?社区是否知道此问题的解决方法?
lambda 函数:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Formatters.Binary;
using Amazon.Lambda.Core;
using Newtonsoft.Json.Linq;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AWSLambda1
{
public class Function
{
public void PostsBasedOnOddOrEven(string input, ILambdaContext context)
{
var details = JObject.Parse(input);
var postId = (int) details["id"];
var oddOrEvenResult = (int) details["OddOrEvenPostsResult"];
}
}
}
从 Step Function 到 Lambda 函数的输入:
{
"id": "1",
"OddOrEvenPostsResult": 2
}
Lambda 函数的输入(通过 Visual Studio 调用工作):
"{ \"id\": \"1\", \"OddOrEvenPostsResult\": 2}"
异常堆栈跟踪:
{
"errorType": "JsonReaderException",
"errorMessage": "Unexpected character encountered while parsing value: {. Path '', line 1, position 1.",
"stackTrace": [
"at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
"at Newtonsoft.Json.JsonTextReader.ReadAsString()",
"at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)",
"at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
"at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)",
"at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
"at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream requestStream)",
"at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
]
}
作为 Step Function 的一部分的 Lambda 函数不工作
Lambda 函数在单独测试时有效
由于您的 lambda 函数期望 input
成为 string
,框架会尝试将输入解析为 string
。
然而,输入实际上是一个JSON对象,而不是字符串。
因此解析器将失败并出现 "unexpected character" 错误。解析器需要一个 "
字符来指示字符串的开头。
那么,您可以按照以下方法解决它:
声明一个代表输入的c#class
public class FunctionInput { public int id { get; set; } public int OddOrEvenPostsResult { get; set; } }
更改函数签名以期望
input
类型为FunctionInput
public class Function { public void PostsBasedOnOddOrEven(FunctionInput input, ILambdaContext context) { var postId = input.id; var oddOrEvenResult = input.OddOrEvenPostsResult; } }
注意:您不需要自己解析输入。