Zoi*_*nky 3 json asp.net-core system.text.json
我有以下内容json被发送到 api
{
"Id":22,
"UserId":22,
"Payload":{
"Field":"some payload value"
...more unknown field/values go here
},
"ContextTypeId":1,
"EventTypeId":1,
"SessionId":1
}
Run Code Online (Sandbox Code Playgroud)
我想将其映射到以下内容:
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; }
}
Run Code Online (Sandbox Code Playgroud)
当映射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();
}
Run Code Online (Sandbox Code Playgroud)
小智 0
您可以使用对象类型而不是字符串。或者使用 Newtonsoft 的 JToken 类型,正如 Ryan 上面已经评论的那样。
公共对象有效负载{获取; 放; }
public class CreateTrackItem : IRequest<int>
{
public int Id { get; set; }
public int UserId { get; set; }
public object Payload { get; set; }
public int ContextTypeId { get; set; }
public int SessionId { get; set; }
public int EventTypeId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15808 次 |
| 最近记录: |