I'm trying to deserialize the below JSON into a list:
{
character_list: [
{
displayname: "Bob (Server 3)",
name: {
first_lower: "bob",
first: "Bob"
},
id: 123
},
{
displayname: "Bobby (Server 12)",
name: {
first_lower: "bobby",
first: "Bobby"
},
id: 1234
},
{
displayname: "Bobert (Server 9)",
name: {
first_lower: "bobert",
first: "Bobert"
},
id: 12345
},
{
displayname: "Bobostic (Server 1)",
name: {
first_lower: "bobostic",
first: "Bobostic "
},
id: 123456
}
]
}
The JSON is stored in a variable called info
. I've got the following line:
var charList = JsonConvert.DeserializeObject<List<Character>>(info);
But when I try, I get the following error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List` because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'character_list', line 1, position 18.
My Character class:
public Character(string json)
{
var jObject = JObject.Parse(json);
var jNow = jObject["character_list"];
Name = (string)jNow.First["name"]["first"];
CharacterId = (int) jNow.First["id"];
DisplayName = (string) jNow.First["displayname"];
}
I've tried searching the site, and most of the answers I saw suggested using the method I'm already using.(i.e., Question 1, Question 2)