C# datacontract json序列化程序中的强制转换异常无效
我正在Visual Studio 2010中为Windows Phone 7.1开发一个项目。我正在尝试下载JSON数据并将其反序列化到对象列表中。下面是我用来构建web请求和处理响应的代码C# datacontract json序列化程序中的强制转换异常无效,c#,json,windows,serialization,windows-phone-7.1,C#,Json,Windows,Serialization,Windows Phone 7.1,我正在Visual Studio 2010中为Windows Phone 7.1开发一个项目。我正在尝试下载JSON数据并将其反序列化到对象列表中。下面是我用来构建web请求和处理响应的代码 public class HttpGetTask<T> { public HttpGetTask(string url, Action<T> onPostExecute) { this.Url = url; this.OnPostExe
public class HttpGetTask<T>
{
public HttpGetTask(string url, Action<T> onPostExecute)
{
this.Url = url;
this.OnPostExecute = onPostExecute;
}
public void Execute()
{
MessageBox.Show("We are in the task Execute method");
if (this.OnPreExecute != null)
{
this.OnPreExecute();
}
// create the http request
HttpWebRequest httpWebRequest = WebRequest.CreateHttp(this.Url);
httpWebRequest.Method = "GET";
httpWebRequest.Accept = "application/json";
// get the response asynchronously
httpWebRequest.BeginGetResponse(OnGetResponseCompleted, httpWebRequest);
}
private void OnGetResponseCompleted(IAsyncResult ar)
{
MessageBox.Show("We are in the OnGetResponseCompleted Method");
var httpWebRequest = (HttpWebRequest)ar.AsyncState;
// get the response
HttpWebResponse response;
try
{
response = (HttpWebResponse)httpWebRequest.EndGetResponse(ar);
}
catch (WebException e)
{
this.InvokeOnErrorHandler("Unable to connect to the web page.");
return;
}
catch (Exception e)
{
this.InvokeOnErrorHandler(e.Message);
return;
}
if (response.StatusCode != HttpStatusCode.OK)
{
this.InvokeOnErrorHandler((int)response.StatusCode + " " + response.StatusDescription);
return;
}
// response stream
var stream = response.GetResponseStream();
// deserialize json
var jsonSerializer = new DataContractJsonSerializer(typeof(T));
var responseObject = (T)jsonSerializer.ReadObject(stream);
// call the virtual method
this.InvokeInUiThread(() => this.OnPostExecute(responseObject));
}
当我运行我的应用程序时,在创建序列化程序对象时,或者在jsonserializer.ReadObject(流)行上,我得到一个无效的强制转换异常。关于发生这种情况的原因,您有什么想法吗?请尝试使用int指定属性:
[DataMember(Name = "oid")]
public string Oid { get; set; }
应该是
[DataMember(Name = "oid")]
public int Oid { get; set; }
尝试使用int指定属性:
[DataMember(Name = "oid")]
public string Oid { get; set; }
应该是
[DataMember(Name = "oid")]
public int Oid { get; set; }