C# 在一个类中反序列化不同的JSON数据
我有两个来自服务器的json。 如果请求正常-返回以下内容:C# 在一个类中反序列化不同的JSON数据,c#,.net,json,json.net,C#,.net,Json,Json.net,我有两个来自服务器的json。 如果请求正常-返回以下内容: {"code":0,"content":{"id":"1318916"}} {"code":5,"content":[]} 如果请求出错-返回以下内容: {"code":0,"content":{"id":"1318916"}} {"code":5,"content":[]} 当请求正常时-本课程表现良好: [JsonObject] public class JsonResponse { [JsonProperty(Pr
{"code":0,"content":{"id":"1318916"}}
{"code":5,"content":[]}
如果请求出错-返回以下内容:
{"code":0,"content":{"id":"1318916"}}
{"code":5,"content":[]}
当请求正常时-本课程表现良好:
[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; }
}
}
[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; }
}
}
当出现错误时-该类工作正常:
[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; }
}
}
[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和错误答案?将错误响应更改为{“代码”:5,“内容”:{}。请注意,我将第一类公共JsonResponseContent内容{get;set;}中的[]更改为{}@3dd,第二类公共JsonResponseContent[]内容{get;set;}中的[]当反序列化第一类的“OK”答案时-无错误,当第一类的“error”答案时-发生错误时-好,对不起,miss read该部分如果在这两种情况下都将其保留为JsonResponseContent[],会发生什么,那么如果该数组是一个错误,那么该数组就是empty@3dd如果将JsonResponseContent[]同时保留在这两个选项中,则当回答“错误”-一切正常,如果回答“确定”-发生错误。如果将错误响应更改为{“代码”:5,“内容”:{},会发生什么情况。注意,我将[]更改为{}