The error I'm getting is as follows:
{"Cannot create and populate list type ReasonCodeList. Path 'AppointmentReasonList[0].ReasonCodeList', line 1, position 162."}
I'm getting this error when converting the following sample JSON data:
{
"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
}
]
}
]
}
I've created the following classes to try and match the data structure.
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; }
}
If, you've made it this far, I apologize for the verbose amount of code but I think all of it was necessary details.
Originally I was getting the same error for this question.
When I adjusted my class structure by adding the ReasonCodeList class I began getting the new error.
The end objective is to extract every Code and Description for every reason object in the JSON and use them as parameters in an SQL Procedure.
This will require two foreach loops and the ReasonCodeList custom collection must implement IEnumerable to do so.
Any help will be greatly appreciated. I'm unfamiliar with the IEnumerable interface and the restrictions surrounding it.
EDIT:
In response to @Mr Hery: The following error occurs when I try to structure the AppointmentReason class as you suggested:
{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type ReasonCodeList' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'AppointmentReasonList[0].ReasonCodeList[0].Code', line 1, position 170."}
I originally had this class structured with the ReasonCodeList attribute being a type of List as that's essentially what it is but got the same error. I tried replacing that with the ReasonCodeList object as was suggested in the referenced answer in the previous original post section.
When I made the changes, the error at the very top of this question is what occurred.
EDIT #2
In response to @Frank Fajardo & his suggestion, this is what the deserialized AppointReasonResponse object will be used for:
jarray = JsonConvert.DeserializeObject<AppointmentReasonResponse>(json);
foreach (AppointmentReason ApptReason in jarray.AppointmentReasonList)
{
foreach (ReasonCode Reason in ApptReason)
{
AddInterfacePMReasonCode(PracticeID, Reason.Code, Reason.Description);
}
}
The attributes for the class ReasonCode due to the following error:
Error 105 foreach statement cannot operate on variables of type 'AppointmentReason' because 'AppointmentReason' does not contain a public definition for 'GetEnumerator'
EDIT 3
As @Frank Fajardo pointed out, the inner foreach loop in Edit #2 was looking at the AppointmentReason object and not its list property. When the code is fixed, an error is returned much like the one I posted originally in this post.
New class object and modified foreach loop:
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);
}
}
Error received:
{"Cannot create and populate list type AppointmentReasonList. Path 'AppointmentReasonList', line 1, position 59."}