如何将一组JSON对象转换为C#列表

时间:2018-06-29 22:38:43

标签: c# json json.net json-deserialization

我有一个看起来像这样的JSON文件:

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

我想将此反序列化为C#类:

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; }
}

使用类似这样的代码进行转换不起作用,因为pets内部有多个对象,而且它不是JSON数组。

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

这里是例外:

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.

有没有一种方法可以将pets对象内部的对象转换为对象数组? (除了将JSON本身传递给DeserializeObject方法之前,还需要对其进行编辑)

3 个答案:

答案 0 :(得分:1)

这是构造错误的json。但是,您可以使用JsonProperty属性将其反序列化,如下所示:

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; }
}

答案 1 :(得分:1)

您的JSON文件包含宠物字典,而不是数组。因此,更改FooObject类:

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();
}

答案 2 :(得分:0)

您的json不会“对象数组” 。它说它有一个具有petsdog属性的cat对象。像这样:

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 ...