gpt4 book ai didi

json - System.Text.Json - JSON 值无法转换为 System.String

转载 作者:行者123 更新时间:2023-12-04 01:00:27 25 4
gpt4 key购买 nike

我有以下 json被发送到 api

{
"Id":22,
"UserId":22,
"Payload":{
"Field":"some payload value"
...more unknown field/values go here
},
"ContextTypeId":1,
"EventTypeId":1,
"SessionId":1
}

我想将其映射到以下内容:
  public class CreateTrackItem : IRequest<int>
{
public int Id { get; set; }
public int UserId { get; set; }

public string Payload { get; set; }
public int ContextTypeId { get; set; }
public int SessionId { get; set; }
public int EventTypeId { get; set; }
}

映射时 Payload属性失败,无法映射 json字符串,我只想要 Payload成为 string版本 json (将进入 jsonb 中的 postgres 字段)

我正在使用 .NET Core 3.0 并且更喜欢使用内置的 System.Text.Json如果可能,在切换到 Newtonsoft 之前.

最佳答案

如果您使用的是 asp.net core 3.0,那么它具有内置的 JSON 支持。我使用了以下内容,它无需设置自定义输入处理程序即可工作。

//// using System.Text.Json;

[HttpPost]
public async Task<IActionResult> Index([FromBody] JsonElement body)
{

string json = System.Text.Json.JsonSerializer.Serialize(body);
return Ok();

}

关于json - System.Text.Json - JSON 值无法转换为 System.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58614796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com