简体   繁体   中英

Deserializing Json Value to C# List<String>

I'm having an issue with converting a one part of a JSON string to C# List. The original code didn't have the [JsonProperty] and it that's where the issue started. I added the [JsonProperty] to the MdId value, but the issue is still happens.

C# Class

public class PendingPatientDocumentRecord
{
    public int PatientDocumentFileMapId;
    public short ContextTypeId;
    public string PatientVisitId;
    public List<string> BillingIds;
    public DateTime? DateOfService;

    [JsonProperty("MdId")]
    public List<string> MdId;
}

Deserialization code

List<PendingPatientDocumentRecord> pendingDocuments = JsonConvert.DeserializeObject<List<PendingPatientDocumentRecord>>(pendingDocumentsJson);

JSON String:

[
  {
    "PatientDocumentFileMapId":12,
    "ContextTypeId":3,
    "DateOfService":"08/31/2022",
    "MdId":"ala"
  }
]

Error:

Error converting value "ala" to type System.Collections.Generic.List`1[System.String]'. Path '[0].MdId'

MdId needs to be an array so that it can be converted to a list. You don't need the JsonProperty attribute.

  {
    "PatientDocumentFileMapId": 12,
    "ContextTypeId": 3,
    "DateOfService": "08/31/2022",
    "MdId": ["ala"]
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM