0
我想反序列化JSON物件:反序列化JSON對象的.Net
"{\"results\":[{\"isError\":true,\"result\":null,\"messages\":[{\"type\":0,\"text\":\"Legal Entity is required when a Cost Center Code is specified: External Code 1 = cc3f25a7-0e8a-4e1c-8e18-c83c520ff3d7;\"}]}],\"isError\":true,\"sessionId\":null,\"tk\":null,\"tp\":null}"
我有以下類別: [DataContract]
public class BigCenterResponse
{
[DataMember(Name = "isError")]
public bool IsError { get; set; }
[DataMember(Name = "sessionId")]
public string SessionID { get; set; }
[DataMember(Name = "results")]
public List<Result> Responses { get; set; }
}
[DataContract]
public class Message
{
[DataMember(Name = "type")]
public int Type { get; set; }
[DataMember(Name = "text")]
public string Text { get; set; }
}
[DataContract]
public class Result
{
[DataMember(Name = "isError")]
public bool IsError { get; set; }
[DataMember(Name="messages")]
public List<Message> Messages { get; set; }
}
而下面的實現:
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
var resp = reader.ReadToEnd();
var serializer = new JavaScriptSerializer();
var obj = serializer.Deserialize<BigCenterResponse>(resp);
obj.Responses = serializer.Deserialize<List<Result>>(resp);
return obj;
}
我能夠正確地設置IsError,但響應沒有被填充。我試過反序列化BigCenterResponse並試圖單獨反序列化結果部分(如上所示)。這兩個似乎都沒有工作。