1

JsonStringを逆シリアル化しようとしています

string JsonString=  "{\"RequestId\":1308,\"Warning\":[\"WARNING_NoOrdersForCustomer\"],\"Customer\":{\"__type\":\"CustomerOrder:#Data\",\"Email\":\"xyz@yahoo.com\",\"FullName\":\"Anke White\",\"Phone\":\"\",\"Orders\":[]}}"

これが私のデータ契約です

 [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; }


      }

これでやってみました

SalesInfo sales = Deserialize<SalesInfo>(JsonString);

これがデシリアライズです

private static T Deserialize<T>(string json)
{
    var instance = Activator.CreateInstance<T>();
    using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
    {
        var serializer = new DataContractJsonSerializer(instance.GetType());
        return (T)serializer.ReadObject(ms);
    }
}

しかし、エラーメッセージが表示されます

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.

このエラーを解決し、JsonStringを逆シリアル化するのを手伝ってください

4

2 に答える 2

1

JsonStringが正しくないため:

\ "Customer \":{ \ "__ type \":\ "CustomerOrder:#Data \"、\ "Em .. ..

また、CustomerOrderタイプに関する情報はありません。

あなたの場合の正しいJsonStringは次のとおりです。

{\" RequestId \ ":1308、\" Warning \ ":[\" WARNING_NoOrdersForCustomer \ "]、\" Customer \ ":{\" Email \ ":\" xyz@yahoo.com \ "、\" FullName \ ":\" Anke White \ "、\" Phone \ ":\" \ "、\" Orders \ ":[]}}

ここに画像の説明を入力してください

于 2013-03-24T20:39:18.877 に答える
0

他のものと互換性のないこれらの「__type」のものを挿入する独自のMSAjaxJSON形式を使用しているようです。

したがって、ソリューションのシリアル化部分を確認してください。

于 2013-03-24T21:17:26.270 に答える