howdoI
howdoI

Reputation: 61

Error converting string to type 'System.Text.Json.JsonElement

I have an class which consist in which i have some problems filling a jsonElement from a json file as such

{
    "entities": [
        {
            "name": "DateTimeENT1",
            "description": "This a  example",
            "uil": {
                      "uill": "This is my Layout"
            }
        }
    ]
}

which is being deserialized into this class:

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;}
}

and this is how I deserialize it:

var input= JsonConvert.DeserializeObject<Container>(File.ReadAllText(@"init.json"));

when i run this I get an error stating that 'Error converting value "This is my Layout" to type 'System.Text.Json.JsonElement'. how do I overcome this?

The weird part about all this is that I can use the same input on my controller endpoint

public IActionResult Put([FromBody] Container container)

which without any problem creates an container, with the given json.. so why does it not work when I do it with the deserializer?

Upvotes: 5

Views: 16757

Answers (2)

Selim Yildiz
Selim Yildiz

Reputation: 5380

You need to use JsonDocument.Parse instead of 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();
}

Output would be:

This is my Layout

Upvotes: 10

piraces
piraces

Reputation: 1348

Interpreting that your JSON is like the one you posted, you should change your Entity class and declare the property of UIL as string:

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;}
}

JsonElement is a struct and I don't know why you may want to map to this struct.

The endpoint is maybe working because it is not mapping that property as you want.

Upvotes: 0

Related Questions