simon
simon

Reputation: 17

JSON C# deserialization

I am having trouble with deserializing data from a JSON string:

[{
    "Example A": [{
            "Parameter1": "Example A",
            "Parameter2": "aaa",
            "Parameter3": "2022-06-30 21:15:00.0000000",
            "Value": 11.11
        }
    ],
    "Example B": [{
            "Parameter1": "Example B",
            "Parameter2": "ccc",
            "Parameter3": "2022-06-30 21:15:00.0000000",
            "Value": 33.33
        }
    ],
    "Example C": [
        {
            "Parameter1": "Example C",
            "Parameter2": "eee",
            "TimestampUTC": "2022-06-30 21:15:00.0000000",
            "Value": null
        },
        {
            "Parameter1": "Example C",
            "Parameter2": "fff",
            "Parameter3": "2022-06-30 21:15:00.0000000",
            "Value": 44.44
        }
    ]
}]

What I have tried is:

public class ExampleA
{
    public string Parameter1 { get; set; }
    public string Parameter2 { get; set; }
    public string Parameter3 { get; set; }
    public double Value { get; set; }
}

public class ExampleB
{
    public string Parameter1 { get; set; }
    public string Parameter2 { get; set; }
    public string Parameter3 { get; set; }
    public double Value { get; set; }
}

public class ExampleC
{
    public string Parameter1 { get; set; }
    public string Parameter2 { get; set; }
    public string Parameter3 { get; set; }
    public double Value { get; set; }
}

public class Examples
{
    [JsonProperty("Example A")]
    public List<ExampleA> ExampleA { get; set; }

    [JsonProperty("Example B")]
    public List<ExampleB> ExampleB { get; set; }

    [JsonProperty("Example C")]
    public List<ExampleC> ExampleC { get; set; }
}

But when I try to test it with this:

Examples someVar = JsonConvert.DeserializeObject<Examples>(JsonString);
Console.WriteLine(SomeVar.ExampleA.Count);

I get this error:

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MyProject.Examples' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Apparently I have trouble understanding how deserialization works - what I want to do with this is something like this:

foreach item in Example
    // do something
    foreach thing in item
        // do something more

I realise I have some trouble with grasping the JSON format in this case.

I'm using .NET Core.

Upvotes: -1

Views: 89

Answers (1)

Serge
Serge

Reputation: 43860

you have an array in your json Try this

List<Examples> someVar = JsonConvert.DeserializeObject<List<Examples>>(JsonString);

Upvotes: 4

Related Questions