5

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?

5
  • 1
    I think UIL and UIV should be string instead of JsonElement? Commented Mar 19, 2020 at 17:07
  • I needs it to be an Some sort of Json which can be initialized by an string? Commented Mar 19, 2020 at 17:11
  • But the values like "This is my Layout" is just string, not JSON object. Commented Mar 19, 2020 at 17:13
  • @SelimYıldız I have an controller endpoint which receives this as an JSON and deserialize it without any problem to container class.. It seem to be a problem when I trigger this with an string Commented Mar 19, 2020 at 17:13
  • I have added an answer, please check. Commented Mar 19, 2020 at 18:28

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

6 Comments

My controllers are able to do so - why am I not able to do so with the deserializer? @piraces
@howdoI I think the controller is not mapping this property... Can you check that?
checked it is though?
and Uill has the value of "This is my Layout"? That's weird... Nevertheless I think you should specify it is a string
UIl has ValueKind = String : "This is my Layou2t" or and yes I tries with a different string
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.