Note that there are some explanatory texts on larger screens.

plurals
  1. POJsonString Deserialization Error
    text
    copied!<p>I am trying to deserialize my JsonString</p> <pre><code>string JsonString= "{\"RequestId\":1308,\"Warning\":[\"WARNING_NoOrdersForCustomer\"],\"Customer\":{\"__type\":\"CustomerOrder:#Data\",\"Email\":\"xyz@yahoo.com\",\"FullName\":\"Anke White\",\"Phone\":\"\",\"Orders\":[]}}" </code></pre> <p>Here are my datacontracts</p> <pre><code> [DataContract] public class SalesInfo { [DataMember(Name = "RequestId")] public string RequestId { get; set; } [DataMember(Name = "Warning")] public string[] Warning { get; set; } [DataMember(Name = "Customer")] public Customer CustomerData { get; set; } } [DataContract] public class Customer { [DataMember(Name = "Email")] public string Email { get; set; } [DataMember(Name = "FullName")] public string FullName { get; set; } [DataMember(Name = "Phone")] public string Phone { get; set; } [DataMember(Name = "Orders")] public string[] Orders { get; set; } } </code></pre> <p>I tried with this </p> <pre><code>SalesInfo sales = Deserialize&lt;SalesInfo&gt;(JsonString); </code></pre> <p>here is the Deserialize</p> <pre><code>private static T Deserialize&lt;T&gt;(string json) { var instance = Activator.CreateInstance&lt;T&gt;(); using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { var serializer = new DataContractJsonSerializer(instance.GetType()); return (T)serializer.ReadObject(ms); } } </code></pre> <p>But I am getting error message </p> <pre><code>Element ':Customer' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/Data:CustomerOrder'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'CustomerOrder' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer. </code></pre> <p>Please help me resolve this error and deserialize JsonString</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload