下面是Json字符串
{
"Resources": {
"Resource": [
{
"ResourceId": "D513E96F-EA6C-E511-8133-000D3A0044F4",
"MSPSLogin": "richa.dinesh.parkar",
"Email": "richa.dinesh.parkar@accenture.com"
},
{
"ResourceId": "D513E96F-EA6C-E511-8133-000D3A0044F4",
"MSPSLogin": "harshal.arun.vadnere",
"Email ": "harshal.arun.vadnere@accenture.com"
}
]
},
"CreatedOn":"2016-07-18T12:51:14.23Z",
"CreatedByApp":"AD"
}
My class in Models:
public class Resource
{
public string ResourceId { get; set; }
public string MSPSLogin { get; set; }
public string Email { get; set; }
}
public class Resources
{
public IList<Resource> Resource { get; set; }
}
public class Example
{
public Resources Resources { get; set; }
public DateTime CreatedOn { get; set; }
public string CreatedByApp { get; set; }
}
我使用Json。我想把下面的字符串转换成Json对象。
Example example = JsonConvert.DeserializeObject<Example>(jsonstr);
但是它出错了:
类型为'Newtonsoft.Json '的未处理异常。JsonSerializationException'在Newtonsoft.Json.dll中发生
附加信息:无法将当前JSON对象(例如{"name":"value"})反序列化为类型'System.Collections.Generic.List ' 1[myWizard_MSPS_integration]。因为该类型需要一个JSON数组(例如[1,2,3])来正确反序列化。
尝试将IList<Resource>
更改为IEnumerable<Resource>
或Resource[]
,我认为Json。Net无法初始化IList
。不支持的特性
将Resources
属性的类型更改为IEnumerable<Resource>
尝试使用这个类模型:
public class Resource
{
public string ResourceId { get; set; }
public string MSPSLogin { get; set; }
public string Email { get; set; }
}
public class Resources
{
public List<Resource> Resource { get; set; }
}
public class Example
{
public Resources Resources { get; set; }
public string CreatedOn { get; set; }
public string CreatedByApp { get; set; }
}
代替 public IList<Resource> Resource { get; set; }
public List<Resource> Resource { get; set; }