【问题标题】:Error converting string to type 'System.Text.Json.JsonElement将字符串转换为类型“System.Text.Json.JsonElement”时出错
【发布时间】: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.. 那么为什么当我使用反序列化器时它不起作用呢?

【问题讨论】:

  • 我认为UILUIV 应该是字符串而不是JsonElement
  • 我需要它是某种可以由字符串初始化的 Json 吗?
  • 但是像"This is my Layout" 这样的值只是字符串,而不是JSON对象。
  • @SelimYıldız 我有一个控制器端点,它以 JSON 形式接收它并将其反序列化,对容器类没有任何问题。当我用字符串触发它时,这似乎是一个问题
  • 我已经添加了答案,请查收。

标签: c# json asp.net-core json-serialization


【解决方案1】:

您需要使用JsonDocument.Parse 而不是JsonConverter.DeserializeObject

static void Main(string[] args)
{
    var jsonInput= @"{
                        ""entities"": 
                        [
                            {
                                ""name"": ""DateTimeENT1"",
                                ""description"": ""This a  example"",
                                ""uil"": {
                                        ""uill"": ""This is my Layout""
                                }
                            }
                        ]
                    }";

    using (JsonDocument doc = JsonDocument.Parse(jsonInput))
    {
        JsonElement root = doc.RootElement;
        JsonElement entities = root.GetProperty("entities");
        //Assuming you have only 1 item, if you have more you can use EnumerateArray and MoveNext()..
        JsonElement uilItem = entities[0].GetProperty("uil");
        JsonElement uillItem = uilItem.GetProperty("uill");
        Console.WriteLine(uillItem.GetString());
    }
    Console.ReadLine();
}

输出将是:

这是我的布局

【讨论】:

    【解决方案2】:

    解释您的 JSON 就像您发布的那样,您应该更改您的 Entity 类并将 UIL 的属性声明为字符串:

    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 {
        public string Uill {get; set;}
    }
    

    JsonElementis a struct 我不知道你为什么要映射到这个结构。

    端点可能正在工作,因为它没有按照您的意愿映射该属性。

    【讨论】:

    • 我的控制器可以这样做 - 为什么我不能使用反序列化器这样做? @piraces
    • @howdoI 我认为控制器没有映射这个属性...你能检查一下吗?
    • 检查过吗?
    • Uill 的值为“这是我的布局”?这很奇怪......不过我认为你应该指定它是一个字符串
    • UIl 有 ValueKind = String : "This is my Layou2t" 或者是的,我尝试使用不同的字符串
    猜你喜欢
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2014-01-23
    相关资源
    最近更新 更多