0

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)

Community
  • 1
  • 1
PiousVenom
  • 6,888
  • 11
  • 47
  • 86
  • 1
    It is not a valid JSON. You can check from this site if it is invalid and which section is not correct. http://jsonlint.com/ – onur Jan 13 '17 at 06:22

2 Answers2

0

You can install Newtonsoft JSON and then your class would look like

public class Name
{
    [JsonProperty("first_lower")]
    public string first_lower { get; set; }
    [JsonProperty("first")]
    public string first { get; set; }
}

public class CharacterList
{
    [JsonProperty("displayname")]
    public string displayname { get; set; }
    [JsonProperty("name")]
    public Name name { get; set; }
    [JsonProperty("id")]
    public int id { get; set; }
}

public class Character
{
    [JsonProperty("character_list")]
    public List<CharacterList> character_list { get; set; }
}

Then Deserialize it like this

string info = File.ReadAllText(YourJSONFile);
var ser = JsonConvert.DeserializeObject<Character>(info);
Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

Isn't the info variable a JSON Object with one property called character_list? Shouldn't you be trying to deserialize info.character_list which is the actual list of characters, rather than info.

This explanation fits the error message also.

GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19