我有一个网络服务,可根据请求输出 JSON 字符串。
目前,我设置了一个接收类,看起来像这样
[DataContract]
public class user_new_register
{
[DataMember(Name="ErrorStatus")]
public string ErrorStatus{ get; set; }
[DataMember(Name="ErrorMessages")]
public string []ErrorMessages { get; set; }
[DataMember(Name="UserID")]
public int UserID { get; set; }
}
我像这样将字符串传递给服务器
var httpReq = (HttpWebRequest)HttpWebRequest.Create (new Uri (createUser));
httpReq.BeginGetResponse ((ar) => {
var request = (HttpWebRequest)ar.AsyncState;
using (var response = (HttpWebResponse)request.EndGetResponse (ar)) {
var s = response.GetResponseStream ();
这有效,“s”包含来自服务器的响应。我现在的问题是如何反序列化对 [DataContract] 类的回复。
我尝试了以下方法,但它不起作用
user_new_register register = new user_new_register ();
var stream = new MemoryStream ();
var serializer = new DataContractJsonSerializer (typeof(json.user_new_register));
var httpReq = (HttpWebRequest)HttpWebRequest.Create (new Uri (createUser));
httpReq.BeginGetResponse ((ar) => {
var request = (HttpWebRequest)ar.AsyncState;
using (var response = (HttpWebResponse)request.EndGetResponse (ar)) {
var s = response.GetResponseStream ();
stream = (JsonObject)JsonObject.Load (s);
register = (user_new_register)serializer.ReadObject (s);
这编译但在 register = 行崩溃
任何帮助或建议将不胜感激:)
谢谢
保罗