1

I have an array of objects like this in json as per below format

[{"GroupID":5},{"GroupID":47}]

What's the correct way to deserialize it?

I have the Group object:

 public class Group
    {
        [JsonProperty("GroupID")]
        public int Id { get; set; }
    }

I'm trying to deserialize by:

Group[] arr = JsonConvert.DeserializeObject<Group[]>(json).Select(j => j.Group).ToArray()

but I get a compiler error - probably due to a missing linking class:

'Group' does not contain a definition for 'Group' and no extension method 'Group' accepting a first argument of type 'Group' could be found (are you missing a using directive or an assembly reference?)

1 Answer 1

3

This:

Select(j => j.Group)

Means: "select the property Group from all the elements in the array".

You don't have a property called Group, you have a class called Group.

All you need is:

Group[] arr = JsonConvert.DeserializeObject<Group[]>(json)
Sign up to request clarification or add additional context in comments.

Comments

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.