下面是一个 Json :
[{
"Result": {
"description": "Application Security Supp Specialist",
"code": "40000003"
}
}, {
"Result": {
"description": "Gvt Cyber Intelligence Specialist",
"code": "40001416"
}
}, {
"Result": {
"description": "Gvt Record Retention Specialist",
"code": "40001428"
}
}]
下面是我创建的类结构,因为我需要将其填充到 C# 对象中。我正在尝试创建RulesEngineOutput的集合并用 json 内容填充它。
public class RulesEngineOutput
{
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
}
public class RulesEngineOutputCollection
{
public IEnumerable<RulesEngineOutput> ProbableRoles { get; set; }
}
我正在尝试使用以下代码来实现这一点:
var bodyJson = JsonConvert.SerializeObject(bodyString);
RulesEngineOutputCollection result = new RulesEngineOutputCollection();
foreach (var item in bodyJson)
{
result = JsonConvert.DeserializeObject<RulesEngineOutputCollection>(item.ToString());
}
但这会引发异常,因为该项目得到一个字符,我认为我需要在循环中传递一个 JSON 对象,但我无法得到一个。每次我得到的是一个 JSON 字符串。
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“RulesEngineOutputCollection”,因为该类型需要 JSON 对象(例如 {\"name\":\"value\"})才能正确反序列化。\r \n要修复此错误,请将 JSON 更改为 JSON 对象(例如 {\"name\":\"value\"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList ) 比如可以从 JSON 数组反序列化的 List。