【发布时间】:2020-03-19 16:55:28
【问题描述】:
我有一个类,其中我在从 json 文件中填充 jsonElement 时遇到一些问题
{
"entities": [
{
"name": "DateTimeENT1",
"description": "This a example",
"uil": {
"uill": "This is my Layout"
}
}
]
}
正在反序列化到这个类中:
public class Container {
public ICollection<Entity> Entities {get; set;}
}
public class Entity {
public string Name {get; set;}
public string Descripton {get; set;}
UIL Uil {get; set;}
}
public class UIL{
JsonElement Uill {get; set;}
}
这就是我反序列化它的方式:
var input= JsonConvert.DeserializeObject<Container>(File.ReadAllText(@"init.json"));
当我运行它时,我收到一条错误消息,指出 'Error converting value "This is my Layout" to type 'System.Text.Json.JsonElement'. 我该如何克服这个问题?
这一切的奇怪之处在于我可以在我的控制器端点上使用相同的输入
public IActionResult Put([FromBody] Container container)
它毫无问题地创建了一个容器,带有给定的 json.. 那么为什么当我使用反序列化器时它不起作用呢?
【问题讨论】:
-
我认为
UIL和UIV应该是字符串而不是JsonElement? -
我需要它是某种可以由字符串初始化的 Json 吗?
-
但是像
"This is my Layout"这样的值只是字符串,而不是JSON对象。 -
@SelimYıldız 我有一个控制器端点,它以 JSON 形式接收它并将其反序列化,对容器类没有任何问题。当我用字符串触发它时,这似乎是一个问题
-
我已经添加了答案,请查收。
标签: c# json asp.net-core json-serialization