【问题标题】:How to read a json input in C# using Newtonsoft.Json如何使用 Newtonsoft.Json 在 C# 中读取 json 输入
【发布时间】:2021-08-27 23:54:08
【问题描述】:

我正在尝试读取进入 AWS lambda 函数的 json,这是下面初始代码的一部分

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

// 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 DemoApp
{
    public class Function
    {
        public string FunctionHandler(dynamic input,ILambdaContext context)
        {
            dynamic results = JsonConvert.DeserializeObject<dynamic>(input);
            String customer = results.customer;
             .......

传入的json是

{'customer': 'Test Customer',
 'phone': '0435 434 544',
 'asset': 'ASSET',
 'po': 'PO09876',
 'location': 'XYZ',
 'items': {'hose': [[{'goods': 'A231',
     'qty': '0.23',
     'backorder': '0.0',
     'unitprice': '0.0',
     'total': '0.0'},
    {'goods': 'B564',
     'qty': '1.0',
     'backorder': '0.0',
     'unitprice': '0.0',
     'total': '0.0'},
    {'goods': 'C544',
     'qty': '1.0',
     'backorder': '0.0',
     'unitprice': '0.0',
     'total': '0.0'}]],
  'items': []}}

我收到一个错误The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject&lt;object&gt;(string)' has some invalid arguments",。如何正确读取json?

【问题讨论】:

  • “传入的 JSON”不是 JSON。 dynamic input 不是 string input(虽然 string 可以通过此参数传入,但编译器无法知道这种情况是否总是如此,或者永远如此),这就是编译器错误。即使你解决了这个问题,你也无法反序列化它,因为它不是有效的 JSON。
  • @Llama 显示的示例缺少引号,因为我复制了在另一个应用程序上生成的内容,但当它进入 lambda 时它是正确的 json,因为 lambda 只接受 json 输入。还是有其他原因导致它无效?
  • 不,没有其他原因。我浏览并添加了所有引号,它看起来很好,所以从这个角度来看你应该很好。
  • @Llama 好的,谢谢。我已经编辑了
  • 您提供的 JSON 使用 Newtonsoft 的 DeserializeObject 调用解析为 JSON(我写了一个小工具来验证 JSON 或 XML - 您的通过)(。但是您遇到的问题是(如 @ Llama 指出),您正在调用DeserializeObject&lt;object&gt;(string),但传递的是dynamic 而不是字符串。将动态转换为字符串(使用(string)inputinput as string(如果输入不是字符串,则第一个将抛出,在这种情况下,第二个将评估为 null),你应该好好去

标签: c# json aws-lambda json.net


【解决方案1】:

JsonConvert.DeserializeObject需要一个字符串,所以我已将 json 转换为字符串,一切正常。

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

// 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 DemoApp
{
    public class Function
    {
        public string FunctionHandler(Object input,ILambdaContext context)
        {
            string jsonString = System.Text.Json.JsonSerializer.Serialize(input);
            dynamic results = JsonConvert.DeserializeObject(jsonString);
            String customer = results.customer;
             .......

【讨论】:

    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 2013-08-24
    • 2022-10-19
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多