2012-11-24 91 views
1

這真的讓我難堪。我試圖反序列化JSON如下字符串這是我從一個ASP.NET Web服務得到:嘗試使用DataContractJsonSerializer對JSON進行反序列化時出錯

"{\"d\":{\"__type\":\"KPCServer.LogonResult\",\"User\":{\"UserId\":\"affaa328-5b53-430e-991a-22674ede6faf\",\"Email\":\"[email protected]\",\"Alias\":\"Mike\",\"FullName\":\"Mike Christensen\",\"Password\":\"secret\",\"Location\":\"Redmond, WA\",\"ImageUrl\":null,\"DateOfBirth\":\"\\/Date(-62135568000000)\\/\",\"LastLogon\":\"\\/Date(1350450228000)\\/\",\"UserSince\":\"\\/Date(1197980020000)\\/\",\"MailingList\":true,\"Bio\":\"Test\"},\"NewUser\":false,\"Ticket\":\"FJEjfje87fjef88fe8FAF8fA88fAjk+AFJ9fja9Fa9Ff99aJF9aFjfA99fjaBFJ7zqmlcHn9Dfw=\"}}" 

我有以下幾種類型:

public class User 
{ 
    public Guid UserId { get; set; } 
    public string Email { get; set; } 
    public string Alias { get; set; } 
    public string FullName { get; set; } 
    public string Password { get; set; } 
    public string Location { get; set; } 
    public string ImageUrl { get; set; } 
    public DateTime DateOfBirth { get; set; } 
    public DateTime LastLogon { get; set; } 
    public DateTime UserSince { get; set; } 
    public bool MailingList { get; set; } 
    public string Bio { get; set; } 
} 

[DataContract(Name="KPCServer.LogonResult")] 
public class LogonResult 
{ 
    [DataMember] public User User { get; set; } 
    [DataMember] public bool NewUser { get; set; } 
    [DataMember] public string Ticket { get; set; } 
} 

[DataContract] 
[KnownType(typeof(LogonResult))] 
public class Result<T> 
{ 
    [DataMember] 
    public T d { get; set; } 
} 

然後我嘗試使用反序列化字符串:

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) 
{ 
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Result<T>)); 
    Result<T> result = serializer.ReadObject(stream) as Result<T>; 

    return result.d; 
} 

注:在上述方法中,TLogonResult類型。

不過,我得到以下異常對ReadObject

System.Runtime.Serialization.SerializationException was unhandled by user code 
    HResult=-2146233076 
    Message=JSON contains a '__type' member specifying the data contract name ':KPCServer.LogonResult'. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'KPCServer.LogonResult' 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. You can also often eliminate this error by avoiding the use of derived types where the JSON is produced. 
    Source=System.ServiceModel.Web 
    InnerException: 

如果我運行:

json = json.Replace("_type", "_blah"); 

然後一切工作正常。這是在Windows Phone 8

回答

3

使用Silverlight這是由於這樣的事實:

"\"__type\":\"KPCServer.LogonResult\"" 

不包含數據合同的命名空間。這是通過修改DataContractAttribute修復LogonResult

[DataContract(Name = "KPCServer.LogonResult", Namespace="")] 
public class LogonResult 
{ 
    [DataMember] 
    public User User { get; set; } 
    [DataMember] 
    public bool NewUser { get; set; } 
    [DataMember] 
    public string Ticket { get; set; } 
} 
+0

廢話!添加'Namespace =「」'實際上解決了問題。我會想到這個,呃,從來沒有.. –

+0

很高興它的工作! –

相關問題