How to Convert a Set of JSON Objects to C# List

0

I have a JSON file that looks something like this:

{
    "foo": "bar",
    "pets": {
        "dog": {
            "name": "spot",
            "age": "3"
        },
        "cat": {
            "name": "wendy",
            "age": "2"
        }
    }
}

I would like to Deserialize this into a C# class:

public class PetObject
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("age")]
    public string Age { get; set; }
}

public class FooObject
{
    [JsonProperty("foo")]
    public string Foo { get; set; }

    [JsonProperty("pets")]
    public List<PetObject> Pets { get; set; }
}

Using some code like this to convert does not work since pets has multiple objects inside it, and is not a JSON Array.

//does not work
FooObject content = JsonConvert.DeserializeObject<FooObject>(json);

Here is the Exception:

Newtonsoft.Json.JsonSerializationException
  HResult=0x80131500
  Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.PetObject]' 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<T>) 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 'pets.dog', line 4, position 10.

Is there a way to convert the objects inside the pets object into an array of objects? (Outside of just editing the JSON itself before passing it to the DeserializeObject method)

c#
json
json.net
json-deserialization
asked on Stack Overflow Jun 29, 2018 by LionsBlade321 • edited Jul 2, 2018 by LionsBlade321

3 Answers

1

This is badly constructed json. However, you can deserialize it using the JsonProperty attribute as follows:

class Program
{
    static void Main(string[] args)
    {
        string json = "{\"foo\": \"bar\",\"pets\": {\"dog\": {\"name\": \"spot\",\"age\": \"3\"},\"cat\": {\"name\": \"wendy\",\"age\": \"2\"}}}";
        Foo content = JsonConvert.DeserializeObject<Foo>(json);

        Console.Read();
    }
}

public class PetObject
{
   [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("age")]
    public string Age { get; set; }
}

public class Foo
{
    [JsonProperty("foo")]
    public string FooStr { get; set; }

    [JsonProperty("pets")]
    public Dictionary<string, PetObject> Pets { get; set; }
}
answered on Stack Overflow Jun 29, 2018 by L_J
1

Your JSON file contains a dictionary of pets, not an array. So, change the FooObject class:

public class FooObject
{
    [JsonProperty("foo")]
    public string Foo { get; set; }

    [JsonProperty("pets")]
    public Dictionary<string, PetObject> Pets { get; set; }

    // if you still need a list of pets, use this
    public List<PetObject> PetsList => Pets.Values.ToList();
}
answered on Stack Overflow Jun 29, 2018 by Groxan • edited Jul 3, 2018 by Groxan
0

Your json does not say an "array of objects". It says it has a pets object with dog and cat properties. So something like this:

public class PetObject
{
    public string name { get; set; }
    public string age { get; set; }
}

public class Foo
{
    public string foo { get; set; }
    public Pets pets {get;set;}

}

public class Pets
{
    public PetObject dog { get; set; }
    public PetObject cat { get; set;}
}

public class Program
{
    public static void Main()
    {
        var json = "{\"foo\": \"bar\",\"pets\": {\"dog\": {\"name\": \"spot\",\"age\": \"3\"},\"cat\": {\"name\": \"wendy\",\"age\": \"2\"}}}"; 

        var content = JsonConvert.DeserializeObject<Foo>(json);

        Console.WriteLine(content.pets.dog.name);   

    }
}

Hth...

answered on Stack Overflow Jun 29, 2018 by EdSF

User contributions licensed under CC BY-SA 3.0