在一个类中反序列化不同的JSON数据

本文关键字:反序列化 数据 JSON 一个 | 更新日期: 2023-09-27 18:05:42

我有两个json从服务器。如果请求是OK的-返回这个:

{"code":0,"content":{"id":"1318916"}}

如果请求是ERROR -返回this:

{"code":5,"content":[]}

当请求是OK -这类好工作:

[JsonObject]
public class JsonResponse
{
   [JsonProperty(PropertyName = "code", Order = 1)]
   public int Code { get; set; }
   [JsonProperty(PropertyName = "content", Order = 2)]
   public JsonResponseContent Content { get; set; }
   public class JsonResponseContent
   {
      public string Id { get; set; }
   }
}

当ERROR - this class good work时:

[JsonObject]
public class JsonResponse
{
   [JsonProperty(PropertyName = "code", Order = 1)]
   public int Code { get; set; }
   [JsonProperty(PropertyName = "content", Order = 2)]
   public JsonResponseContent[] Content { get; set; }
   public class JsonResponseContent
   {
      public string Id { get; set; }
   }
}

OK和ERROR答案可以合并到一个类吗?

在一个类中反序列化不同的JSON数据

将错误响应更改为{"code":5,"content":{}}。注意,我将[]更改为{}