C# json.net将字符串反序列化为嵌套类
我从http请求接收到一个Json字符串,该字符串如下所示:C# json.net将字符串反序列化为嵌套类,c#,json,json.net,C#,Json,Json.net,我从http请求接收到一个Json字符串,该字符串如下所示: { "info": [ { "calls":0, "errors":"[error1, error2, error3]", "messages":0, "mail":3 } ], "received":5, "valid":3 } 我试图反序列化到的实体的结构大致相同 class Resp
{
"info":
[
{
"calls":0,
"errors":"[error1, error2, error3]",
"messages":0,
"mail":3
}
],
"received":5,
"valid":3
}
我试图反序列化到的实体的结构大致相同
class ResponseEntity
{
private Info info;
private int received;
private int valid;
[JsonProperty("info")]
public Info Info
{
get { return info; }
set { info = value; }
}
[JsonProperty("valid")]
public int valid
{
get { return valid; }
set { valid = value; }
}
[JsonProperty("received")]
public int received
{
get { return received; }
set { received = value; }
}
public class Info
{
private int calls;
private List<string> errors;
private int messages;
private int mail;
[JsonProperty("calls")]
public int Calls
{
get { return calls; }
set { calls = value; }
}
[JsonProperty("messages")]
public int Messages
{
get { return messages; }
set { messages = value; }
}
[JsonProperty("errors")]
public List<string> Errors
{
get { return errors; }
set { errors = value; }
}
[JsonProperty("mail")]
public int Mail
{
get { return mail; }
set { mail = value; }
}
}
}
类响应性
{
私人信息;
收到私人int;
私有int有效;
[JsonProperty(“信息”)]
公共信息
{
获取{返回信息;}
设置{info=value;}
}
[JsonProperty(“有效”)]
公共整数有效
{
获取{return valid;}
设置{valid=value;}
}
[JsonProperty(“已收到”)]
收到公共整数
{
获取{return received;}
设置{received=value;}
}
公共类信息
{
私人通话;
私有列表错误;
私人信息;
私人信件;
[JsonProperty(“调用”)]
公共整数呼叫
{
获取{返回调用;}
设置{calls=value;}
}
[JsonProperty(“消息”)]
公共int消息
{
获取{返回消息;}
设置{messages=value;}
}
[JsonProperty(“错误”)]
公开列表错误
{
获取{返回错误;}
设置{errors=value;}
}
[JsonProperty(“邮件”)]
公共int邮件
{
获取{返回邮件;}
设置{mail=value;}
}
}
}
当我尝试反序列化它时,尽管我得到了一个异常
ResponseEntity ent = JsonConvert.DeserializeObject<ResponseEntity>(json) as ResponseEntity;
Cannot deserialize JSON array into type 'CSharpRestService.ResponseEntity+Info'.
ResponseEntity=JsonConvert.DeserializeObject(json)作为ResponseEntity;
无法将JSON数组反序列化为“CSharpRestService.ResponseEntity+Info”类型。
有人能看出我做错了什么吗?我认为“errors”json键会把事情搞砸,但我也尝试了一个字符串数组。我的测试代码无法使用嵌套的Info类编译(由于属性命名冲突),因此我将其从ResposeEntity类中删除 除此之外,我还修复了JSON的一些问题(您的info对象是一个数组,错误数组中的字符串需要加引号) 见下文: JSON
{
info":
{
"calls":0,
"errors":["error1", "error2", "error3"],
"messages":0,
"mail":3
},
"received":5,
"valid":3
}
课程
class ResponseEntity
{
private Info info;
private int received;
private int valid;
[JsonProperty("info")]
public Info Info
{
get { return info; }
set { info = value; }
}
[JsonProperty("valid")]
public int Valid
{
get { return valid; }
set { valid = value; }
}
[JsonProperty("received")]
public int Received
{
get { return received; }
set { received = value; }
}
}
public class Info
{
private int calls;
private List<string> errors;
private int messages;
private int mail;
[JsonProperty("calls")]
public int Calls
{
get { return calls; }
set { calls = value; }
}
[JsonProperty("messages")]
public int Messages
{
get { return messages; }
set { messages = value; }
}
[JsonProperty("errors")]
public List<string> Errors
{
get { return errors; }
set { errors = value; }
}
[JsonProperty("mail")]
public int Mail
{
get { return mail; }
set { mail = value; }
}
}
类响应性
{
私人信息;
收到私人int;
私有int有效;
[JsonProperty(“信息”)]
公共信息
{
获取{返回信息;}
设置{info=value;}
}
[JsonProperty(“有效”)]
公共整数有效
{
获取{return valid;}
设置{valid=value;}
}
[JsonProperty(“已收到”)]
收到公共整数
{
获取{return received;}
设置{received=value;}
}
}
公共类信息
{
私人通话;
私有列表错误;
私人信息;
私人信件;
[JsonProperty(“调用”)]
公共整数呼叫
{
获取{返回调用;}
设置{calls=value;}
}
[JsonProperty(“消息”)]
公共int消息
{
获取{返回消息;}
设置{messages=value;}
}
[JsonProperty(“错误”)]
公开列表错误
{
获取{返回错误;}
设置{errors=value;}
}
[JsonProperty(“邮件”)]
公共int邮件
{
获取{返回邮件;}
设置{mail=value;}
}
}
测试代码
string json = "{\"info\":{\"calls\":0,\"errors\":[\"error1\", \"error2\", \"error3\"],\"messages\":0,\"mail\":3},\"received\":5,\"valid\":3}";
ResponseEntity ent = JsonConvert.DeserializeObject<ResponseEntity>(json) as ResponseEntity;
string json=“{\'info\':{\'calls\':0,\'errors\':[\'error1\',\'error2\',\'error3\'],\'messages\':0,\'mail\':3},\'received\':5,\'valid\':3};
ResponseEntity=JsonConvert.DeserializeObject(json)作为ResponseEntity;
希望这有帮助。属性名称的首字母应大写。请参阅“valid”和“received”谢谢,我没有想到问题会出在实际的JSON上,我没有仔细观察。我已经联系了向我发送数据的提供商,他们将对其进行修复。