我得到的错误如下:
{“无法创建和填充列表类型ReasonCodeList. Path 'AppointmentReasonList.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对象(例如,{\”ReasonCodeList\“:\”值“})为类型.NET‘,因为该类型需要一个JSON数组(例如,1,2,3)反序列化。\r\n要修复此错误,可以将JSON更改为JSON数组(例如,1,2,3),或者将反序列化类型更改为正常的.NET类型(例如,不像整数这样的原始类型,而不是可以从JSON对象反序列化的集合类型,如数组或列表)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。\r\n nPath 'AppointmentReasonList.ReasonCodeList.Code',第1行,位置170。
我最初是用ReasonCodeList属性作为列表的一种类型来构造这个类的,因为它本质上就是这样的,但是得到了相同的错误。我试着用ReasonCodeList对象代替它,就像上一篇文章中引用的答案中所建议的那样。
当我进行更改时,这个问题最上面的错误是发生了什么。
编辑#2
针对@ Fajardo &他的建议,反序列化的AppointReasonResponse对象将用于:
jarray = JsonConvert.DeserializeObject<AppointmentReasonResponse>(json);
foreach (AppointmentReason ApptReason in jarray.AppointmentReasonList)
{
foreach (ReasonCode Reason in ApptReason)
{
AddInterfacePMReasonCode(PracticeID, Reason.Code, Reason.Description);
}
}由于以下错误导致的类ReasonCode的属性:
Error 105 foreach语句不能对“AppointmentReason”类型的变量进行操作,因为“AppointmentReason”不包含“GetEnumerator”的公共定义
编辑3
正如@ 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”}
发布于 2018-04-13 14:57:03
错误说cannot create and populate list是因为您在代码中缺少了List<>。以下是更正:
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 List<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; }
}您的JSON返回数组数据,因此您需要将List<>
https://stackoverflow.com/questions/49819521
复制相似问题