【发布时间】:2018-05-01 00:01:38
【问题描述】:
我得到的错误如下:
{"无法创建和填充列表类型 ReasonCodeList。路径 'AppointmentReasonList[0].ReasonCodeList',第 1 行,位置 162。"}
转换以下示例 JSON 数据时出现此错误:
{
"ErrorCode":""
,"ErrorMessage":""
,"AppointmentReasonList":[
{
"ClassCode":"851"
,"ClassDescription":"newpat"
,"Active":true
,"IsPatientRelated":true
,"ReasonCodeList":[
{
"Code":"851"
,"Description":"Emergency New Patient Visit"
,"Duration":15
,"Active":true
}
,{
"Code":"BH NEW"
,"Description":"BH NEW"
,"Duration":15
,"Active":true
}
]
}
,{
"ClassCode":"ANE"
,"ClassDescription":"Anesthesia"
,"Active":true
,"IsPatientRelated":true
,"ReasonCodeList":[
{
"Code":"123456"
,"Description":"This is only a test"
,"Duration":15
,"Active":true
}
]
}
]
}
我创建了以下类来尝试匹配数据结构。
public class AppointmentReasonResponse
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
public List<AppointmentReason> AppointmentReasonList { get; set; }
}
public class AppointmentReason
{
public string ClassCode { get; set; }
public string ClassDescription { get; set; }
public bool Active { get; set; }
public bool IsPatientRelated { get; set; }
public ReasonCodeList ReasonCodeList { get; set; }
}
public class ReasonCodeList:IEnumerable<ReasonCode>
{
public List<ReasonCode> ReasonCodeLi { get; set; }
IEnumerator<ReasonCode> IEnumerable<ReasonCode>.GetEnumerator()
{
// Return the array object's IEnumerator.
foreach (ReasonCode Reason in ReasonCodeLi)
{
yield return Reason;
}
}
public IEnumerator<ReasonCode> GetEnumerator()
{
// Return the array object's IEnumerator.
return ReasonCodeLi.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
// call the generic version of the method
return this.GetEnumerator();
}
}
public class ReasonCode
{
public string Code { get; set; }
public string Description { get; set; }
public int Duration { get; set; }
public bool Active { get; set; }
}
如果你已经做到了这一步,我为冗长的代码道歉,但我认为所有这些都是必要的细节。
最初我遇到了同样的错误question。
当我通过添加 ReasonCodeList 类来调整我的类结构时,我开始收到新错误。
最终目标是提取 JSON 中每个原因对象的每个代码和描述,并将它们用作 SQL 过程中的参数。
这将需要两个 foreach 循环,并且 ReasonCodeList 自定义集合必须实现 IEnumerable 才能这样做。
任何帮助将不胜感激。我不熟悉 IEnumerable 接口及其周围的限制。
编辑:
回复@Mr Hery:当我尝试按照您的建议构建 AppointmentReason 类时出现以下错误:
{"无法反序列化当前的 JSON 对象(例如 {\"name\":\"value\"}) 输入类型 ReasonCodeList' 因为类型需要 正确反序列化的 JSON 数组(例如 [1,2,3])。\r\n修复此问题 错误将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改 反序列化的类型,使其成为普通的 .NET 类型(例如,不是 像整数这样的原始类型,而不是像数组这样的集合类型或 List) 可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型中以强制它 从 JSON 对象反序列化。\r\n路径 'AppointmentReasonList[0].ReasonCodeList[0].Code',第 1 行,位置 170."}
我最初使用 ReasonCodeList 属性作为 List 类型构造了这个类,因为它本质上就是这样,但得到了同样的错误。我尝试用上一个原始帖子部分中引用的答案中建议的 ReasonCodeList 对象替换它。
当我进行更改时,这个问题最顶部的错误就是发生了什么。
编辑#2
响应@Frank Fajardo 和他的建议,这就是反序列化的 ApppointReasonResponse 对象的用途:
jarray = JsonConvert.DeserializeObject<AppointmentReasonResponse>(json);
foreach (AppointmentReason ApptReason in jarray.AppointmentReasonList)
{
foreach (ReasonCode Reason in ApptReason)
{
AddInterfacePMReasonCode(PracticeID, Reason.Code, Reason.Description);
}
}
原因代码类的属性由于以下错误:
错误 105 foreach 语句不能对类型变量进行操作 'AppointmentReason' 因为 'AppointmentReason' 不包含 'GetEnumerator' 的公共定义
编辑 3
正如@Frank Fajardo 所指出的,Edit #2 中的内部 foreach 循环查看的是 AppointmentReason 对象,而不是它的 list 属性。修复代码后,返回的错误很像我最初在这篇文章中发布的错误。
新的类对象和修改的foreach循环:
public class AppointmentReasonResponse
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
public AppointmentReasonList AppointmentReasonList { get; set; }
}
public class AppointmentReasonList : IEnumerable<AppointmentReason>
{
public List<AppointmentReason> AppointmentReasonLi { get; set; }
IEnumerator<AppointmentReason> IEnumerable<AppointmentReason>.GetEnumerator()
{
foreach (AppointmentReason ApptReason in AppointmentReasonLi)
{
yield return ApptReason;
}
}
public IEnumerator<AppointmentReason> GetEnumerator()
{
return AppointmentReasonLi.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
foreach (AppointmentReason ApptReason in jarray.AppointmentReasonList)
{
foreach (ReasonCode Reason in ApptReason.ReasonCodeList)
{
AddInterfacePMReasonCode(PracticeID, Reason.Code, Reason.Description);
}
}
收到错误:
{"无法创建和填充列表类型 AppointmentReasonList.Path 'AppointmentReasonList',第 1 行,位置 59。"}
【问题讨论】:
-
请把反序列化的代码也放上去。
-
public ReasonCodeList ReasonCodeList { get; set; }必须是public List<ReasonCodeList> ReasonCodeList { get; set; } -
您是否已清除构建然后重新构建?如果错误仍然出现,也许可以尝试修改
ReasonCodeList类的枚举。 -
另外不要忘记检查来自
ReasonCodeList的数据。它们必须与 JSON 数据相等。 -
@Mr Hery 我按照您的建议清理并重建了项目,并且遇到了同样的错误。此外,我使用我提供的示例 JSON 响应仔细检查了 ReasonCodeResponse 以及其他类对象。我没有看到任何不一致的地方
标签: c# json.net deserialization ienumerable