【问题标题】:Deserialize a nested JSON string in AWS Lambda (C#)在 AWS Lambda (C#) 中反序列化嵌套的 JSON 字符串
【发布时间】:2021-04-12 03:34:09
【问题描述】:

我有一个嵌套的 JSON 字符串,它传递给 lambda(状态机的一部分)。我需要为字典中的每个项目分配变量,然后在 Lambda 过程中进一步使用这些变量(此处未显示)。我已经能够编写几个类和一个 void 来尝试反序列化 json,但我不知道如何将 DeserialiseJSONString 集成到 lambda 处理程序 FunctionHandler 中。我对 Python 的熟悉似乎无助于理解如何在这里使用类/函数。任何投入将不胜感激。我正在寻找最简单的方法(如果可能的话)。这是我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace LambdaFunctionTwo
{
    public class Function
    {

        /// <summary>
        /// A simple hello world function.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public string FunctionHandler(NewUserInfo input, ILambdaContext context)
        {
            
            var Status = input.Status;
            var Platform = input.Platform;
            var GatewayID = input.GatewayID;
            var DatasourceID = input.DatasourceID;
            var Modulus = input.Modulus;
            var Exponent = input.Exponent;
            var Time = input.Time;

            LambdaLogger.Log($"Calling function name:   {context.FunctionName}\n");
            LambdaLogger.Log($"status:                  {Status}\n");
            LambdaLogger.Log($"platform:                {Platform}\n");
            LambdaLogger.Log($"gatewayID:               {GatewayID}\n");
            LambdaLogger.Log($"datasourceID:            {DatasourceID}\n");
            LambdaLogger.Log($"modulus:                 {Modulus}\n");
            LambdaLogger.Log($"exponent:                {Exponent}\n");
            LambdaLogger.Log($"time:                    {Time}\n");
            return $"{Platform} has been updated!";
        }
        public class NewUserInfo
        {
            public string Status { get; set; }
            public string Platform { get; set; }
            public string GatewayID { get; set; }
            public string DatasourceID { get; set; }
            public string Modulus { get; set; }
            public string Exponent { get; set; }
            public string Time { get; set; }

            public class user
            {
                public string Platform { get; set; }
                public string GatewayID { get; set; }
                public string DatasourceID { get; set; }
                public string Modulus { get; set; }
                public string Exponent { get; set; }
                public string Time { get; set; }
            }
        }

        public void DeserialiseJSONString(string strJSON)
        {
            var Credentials = JsonConvert.DeserializeObject<NewUserInfo>(strJSON);
            var Status = Credentials.Status;
            var Platform = Credentials.Platform;
            var GatewayID = Credentials.GatewayID;
            var DatasourceID = Credentials.DatasourceID;
            var Modulus = Credentials.Modulus;
            var Exponent = Credentials.Exponent;
            var Time = Credentials.Time;
        }
    }
}

下面是传递给处理程序的事件的样子:

{
  "status": "message processed",
  "credentials": {
    "platform": "database_platform",
    "gatewayID": "43534-534534g-23423423-1232",
    "datasourceID": "addr3024023-3e423423d-32423423-dfasd",
    "modulus": "thisisssomemodulus34242342342342342346453454125678765978",
    "exponent": "fakeexponent",
    "time": "2021-01-05 15:48:52.417653"
  }
}

请客气,C# 对我来说是全新的!

编辑:

更新代码(构建正常,测试失败)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
// demo
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
//[assembly: Amazon.Lambda.Core.LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace LambdaFunctionTwo
{
    public class Function
    {

        /// <summary>
        /// A simple hello world function.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public string FunctionHandler(string input, ILambdaContext context)
        {
            var myDeserializedClass = JsonConvert.DeserializeObject<Root>(input);
            var Status = myDeserializedClass.Status;
            var Platform = myDeserializedClass.Credentials.Platform;
            var GatewayID = myDeserializedClass.Credentials.GatewayID;
            var DatasourceID = myDeserializedClass.Credentials.DatasourceID;
            var Modulus = myDeserializedClass.Credentials.Modulus;
            var Exponent = myDeserializedClass.Credentials.Exponent;
            var Time = myDeserializedClass.Credentials.Time;

            LambdaLogger.Log($"Calling function name:   {context.FunctionName}\n");
            LambdaLogger.Log($"status:                  {Status}\n");
            LambdaLogger.Log($"platform:                {Platform}\n");
            LambdaLogger.Log($"gatewayID:               {GatewayID}\n");
            LambdaLogger.Log($"datasourceID:            {DatasourceID}\n");
            LambdaLogger.Log($"modulus:                 {Modulus}\n");
            LambdaLogger.Log($"exponent:                {Exponent}\n");
            LambdaLogger.Log($"time:                    {Time}\n");
            return $"{myDeserializedClass} has been updated!";
        }
        public class Credentials
        {
            public string Platform { get; set; }
            public string GatewayID { get; set; }
            public string DatasourceID { get; set; }
            public string Modulus { get; set; }
            public string Exponent { get; set; }
            public string Time { get; set; }
        }

        public class Root
        {
            public string Status { get; set; }
            public Credentials Credentials { get; set; }
        }
    }
}

错误:

System.Exception: Error deserializing the input JSON to type String at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.BuildParameters(ExecutionRequest request, ILambdaContext context) in C:\codebase\aws-lambda-dotnet\Tools\LambdaTestTool\src\Amazon.Lambda.TestTool\Runtime\LambdaExecutor.cs:line 214
   at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.ExecuteAsync(ExecutionRequest request) in C:\codebase\aws-lambda-dotnet\Tools\LambdaTestTool\src\Amazon.Lambda.TestTool\Runtime\LambdaExecutor.cs:line 52
---------------- Inner 1 Exception ------------
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.BuildParameters(ExecutionRequest request, ILambdaContext context) in C:\codebase\aws-lambda-dotnet\Tools\LambdaTestTool\src\Amazon.Lambda.TestTool\Runtime\LambdaExecutor.cs:line 210
---------------- Inner 2 Exception ------------
Amazon.Lambda.Serialization.SystemTextJson.JsonSerializerException: Error converting the Lambda event JSON payload to a string. JSON strings must be quoted, for example "Hello World" in order to be converted to a string: The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
   at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)
---------------- Inner 3 Exception ------------
System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.ParseCore(ReadOnlySpan`1 utf8Json, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](ReadOnlySpan`1 utf8Json, JsonSerializerOptions options)
   at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)
---------------- Inner 4 Exception ------------
System.InvalidOperationException: Cannot get the value of a token type 'StartObject' as a string.
   at System.Text.Json.Utf8JsonReader.GetString()
   at System.Text.Json.Serialization.Converters.JsonConverterString.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
   at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)

【问题讨论】:

  • 你可以使用json2csharp.com为你的json创建类
  • 感谢 Chetan 的帮助,这是我不知道的事情!干杯!

标签: c# json aws-lambda


【解决方案1】:

我将总结问题,然后解释我如何解决这个问题的步骤。这并不难,但是新手可能会错过一些技巧。

问题:

给定一些 json 字符串,我怎样才能轻松地将 json 字符串反序列化为一个类(带有子/嵌套类等)

答案:

如果我们有 json 架构(您确实提供了),我们需要执行以下操作:

  • 创建一些类(数据也将被持久化)
  • 获取json文本
  • 反序列化
  • 测试/断言。

1 - 创建类。

就像@chetanRanpariya 所说的那样,让我们​​使用一个为我们生成这些的网站。当然,有一个小的 json 可能是微不足道的,但是当你得到一个大的 json 模式时,这会为你节省大量的时间。

前往https://json2csharp.com/ 并将您的 json 架构粘贴到其中,然后要求该站点生成输出。

即使在输出中,它们也包含一个示例代码行来实际反序列化它!

root 类是一个垃圾名称,所以我通常将它重命名为 json 模式(数据)的含义。对于此示例,我将保留它.. 但我会将其重命名为 message 或其他名称。

好的,我们已经将我们的类生成为 C# 代码......现在让我们玩一玩吧。

2 测试代码

为了测试简单的 C# 代码,我接下来使用另一个名为 DotNet Fiddler 的网站。

在当前的“fiddle”中,你可以看到我:

  • 设置 json 消息
  • 将此消息反序列化为root 类。

Newtonsoft nuget 库足够聪明,可以解决所有问题。

让我们看看我做了什么,这样你就不会错过一些技巧,让这个工作:

按照我做的事情的顺序

  • 将其更改为 NET5.0
  • 添加了Newtonsoft.Json nuget 库(最新版本)
  • 从 Json2CSharp 复制/粘贴类。请注意我是如何将它们放在 Main 方法下的,在这里?
  • 从 Json2CSharp 添加了一行代码 -> Root myDeserializedClass = JsonConvert.DeserializeObject&lt;Root&gt;(myJsonResponse);
  • 注意到代码看不懂JsonConvert,所以我在文件顶部添加了那个命名空间->using Newtonsoft.Json;
  • 现在,创建了我的假 json 文本,然后将该值传递给反序列化方法(注意方法名称中的变量名称是 (json)
  • 创建了几行代码来打印出 json 反序列化到的类的值。

瞧!我们有一些反序列化的 json 并且可以正常工作。

新手技巧:注意到奇怪的 json 字符串了吗?它是怎么回事:var json = @" ... @ 使字符串成为多行字符串(也称为 逐字字符串文字 或其他东西)。当您这样做时,通过多行读取大量字符串数据会更容易......但我们必须双引号任何"(引号)。通常,我们转义引号inside字符串(例如\")但多行不喜欢任何转义。这是一个文字字符串..因此得名。

【讨论】:

  • 您在彻底解释该过程方面做得非常出色,我非常感谢,但是,在我的帖子中使用 EDIT 的 VS Mock Lambda 测试工具中仍然出现错误。对这个问题有任何见解吗?
  • 嗨@Matt_Davis - 当然,让我们看看这里。您发布的异常包括内部异常。所以我们做到底......然后向后工作。底部是 first 异常..上面是第二个(基于第一个)等等。查看第一个(底部)异常,这表明解析一些错误JSON。关于名为 'StartObject' 的键/值?您能否发布该测试尝试执行的完整 json。让我们确保我们的 JSON 与原始帖子相同,并且其中包含正确的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
相关资源
最近更新 更多