[英]How to read a json input in C# using Newtonsoft.Json
我試圖讀取進入 AWS lambda function 的 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<object>(string)' has some invalid arguments",
。如何正確讀取 json?
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;
.......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.