-1

I have this json

[
            {
               "internalName": "a"
            }
          ]

And a model

public class UC
{
    public string InternalName { get; set; }
}

which was generated from QuickType

but I cannot seem to deserialize this using system.text.json?

this is triggering is causing an error

UC u = JsonSerializer.Deserialize<UC>(json)
kafka
  • 573
  • 1
  • 11
  • 28
  • Think you're missing the attribute `[JsonProperty("internalName")]`, mind the casing. – Dennis VW May 10 '20 at 10:45
  • 3
    Additionally, your JSON represents a *list* rather than a single item. – Jon Skeet May 10 '20 at 10:47
  • @JonSkeet the list only contain one property. I have done this with others.. this is the only one which need this? – kafka May 10 '20 at 10:48
  • 2
    @kafka: Sure, it only contains one object, but it's still a list. Once you've added the `JsonProperty` attribute (because System.Text.Json is case-sensitive) you should try deserializing to `List`. You should end up with a list containing a single element. – Jon Skeet May 10 '20 at 10:55

1 Answers1

3

Your json string represents array not a single object, try var us = JsonSerializer.Deserialize<List<UC>>(json)

Also do not forget to mark property with [JsonPropertyName("internalName")] attribute as stated in the comments(or via JsonSerializerOptions as you mentioned yourself ☺️).

Guru Stron
  • 102,774
  • 10
  • 95
  • 132