无法转换具有空类型的json

时间:2018-06-27 15:07:42

标签: c# json json.net

我需要使用NewtonsoftJson反序列化此json:

{
    "players": [
    {
        "player_id": "331574",
        "team_id": "95",
    },
    {  
        "player_id": "331575",
        "team_id": "95",
    }],
    "coach": 
     {
        "id": "249197",
        "first_name": "Guillermo",
    }
}

所以我有这个课程:

 public class Squad
    {
        public List<Player> players { get; set; }
        public Coach coach { get; set; }

        public class Coach
        {
            public int id { get; set; }
            public string first_name { get; set; }
        }

        public class Player
        {
            public int player_id { get; set; }
            public int team_id { get; set; }
        }
    }

我反序列化使用:

return JsonConvert.DeserializeObject<Squad>(content);

现在有时候我让教练为空:

 {
    "players": [
    {
        "player_id": "331574",
        "team_id": "95",
    },
    {  
        "player_id": "331575",
        "team_id": "95",
    }],
    "coach": []
  }

因此,我实现了此问题中建议的转换器:How to handle both a single item and an array for the same property using JSON.net

尤其是,我所做的唯一编辑是:

[JsonConverter(typeof(SingleOrArrayConverter<object>))]
 public Coach coach { get; set; }

但是反序列化时出现此错误:

  

不能在'Coach'类型上强制转换'System.Collections.Generic.List`1 [System.Object]'类型的对象。

1 个答案:

答案 0 :(得分:1)

您只需要将Coach更改为List<Coach>

class Program
{
    static void Main(String[] args)
    {
        var str1 = "{\"players\":[{\"player_id\":\"331574\",\"team_id\":\"95\",},{\"player_id\":\"331575\",\"team_id\":\"95\",}],\"coach\":{\"id\":\"249197\",\"first_name\":\"Guillermo\",}}";
        var obj1 = JsonConvert.DeserializeObject<Squad>(str1);

        var str2 = "{\"players\":[{\"player_id\":\"331574\",\"team_id\":\"95\",},{\"player_id\":\"331575\",\"team_id\":\"95\",}],\"coach\":[]}";
        var obj2 = JsonConvert.DeserializeObject<Squad>(str2);
    }
}

public class Squad
{
    public List<Player> players { get; set; }

    [JsonProperty("coach")]
    [JsonConverter(typeof(SingleOrArrayConverter<Coach>))]
    public List<Coach> coach { get; set; }
    public class Coach
    {
        public int id { get; set; }
        public string first_name { get; set; }
    }

    public class Player
    {
        public int player_id { get; set; }
        public int team_id { get; set; }
    }
}

class SingleOrArrayConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(List<T>));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);
        if (token.Type == JTokenType.Array)
        {
            return token.ToObject<List<T>>();
        }
        return new List<T> { token.ToObject<T>() };
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}