- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个传递给 lambda(状态机的一部分)的嵌套 JSON 字符串。我需要为字典中的每个项目分配变量,然后在 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)
最佳答案
我将总结这个问题,然后解释我如何解决这个问题的步骤。这并不难,但新手可能会错过一些技巧。
给定一些 json 字符串,我如何轻松地将 json 字符串反序列化为一个类(带有子类/嵌套类等)
如果我们有 json 架构(您确实提供了),我们需要执行以下操作:
正如@chetanRanpariya 所说,让我们使用一个为我们生成这些的网站。当然,拥有一个小的 json 可能是微不足道的,但是当您获得一个大的 json 模式时,这将为您节省大量时间。
前往 https://json2csharp.com/并将您的 json 模式粘贴到那里,然后要求该站点生成输出。
即使在输出中,它们也包含一行示例代码来实际反序列化它!
root
class 是一个废话名称,所以我通常将其重命名为 json 模式(数据)的含义。对于这个例子,我会保留它..但我会把它重命名为 message
什么的。
好的,我们已经将我们的类生成为 C# 代码..现在让我们玩一玩吧。
为了测试简单的 C# 代码,我接下来使用另一个名为 DotNet Fiddler 的网站.
在当前的“fiddle”中,您可以看到我:
root
类。Newtonsoft nuget 库足够聪明,可以解决所有问题。
让我们看看我做了什么,这样您就不会错过一些技巧,让这个工作正常进行:
按照我做的事情的顺序
Newtonsoft.Json
nuget库(最新版)Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
JsonConvert
,所以我在文件顶部添加了该命名空间 -> using Newtonsoft.Json;
(json)
瞧!我们有一些 json 反序列化和工作。
新手技巧:注意到奇怪的 json 字符串了吗?它是怎样的:var json = @"
... 那 @
使字符串成为多行字符串(也称为 verbatim string literal 或其他名称)。当您这样做时,通过多行读取大量字符串数据会更容易。但是我们必须双引号任何 "
(引号)。通常,我们转义一个引号在一个字符串中(例如\"
)但是多行不喜欢任何转义。这是一个文字字符串..因此得名。
关于c# - 在 AWS Lambda (C#) 中反序列化嵌套的 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65594910/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!