【问题标题】:invalid casting exception from datacontract json serializer来自数据合同 json 序列化程序的无效转换异常
【发布时间】:2017-03-07 21:03:09
【问题描述】:

我正在为 Visual Studio 2010 中的 Windows Phone 7.1 开发一个项目。我正在尝试下载 JSON 数据并将其反序列化为对象列表。以下是我用来构建 Web 请求和处理响应的代码。

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

以下是我正在使用的 DataContract 类。

[DataContract]
public class OwnersList
{
    public List<Owner> Owners {get; set; }
}

[DataContract]
public class Owner
{
    [DataMember(Name = "oid")]
    public string Oid { get; set; }

    [DataMember(Name = "fname")]
    public string Fname { get; set; }

    [DataMember(Name = "lname")]
    public string Lname { get; set; }
}

以下是我尝试反序列化的 JSON 数据示例。

[{"oid":"1","fname":"John","lname":"Doe"},{"oid":"2","fname":"Mary","lname":"Smith"},{"oid":"3","fname":"Jimi","lname":"Hendrix"},{"oid":"4","fname":"Carole","lname":"King"},{"oid":"5","fname":"John","lname":"Winchester"},{"oid":"6","fname":"John","lname":"Hurt"},{"oid":"7","fname":"Rick","lname":"Grimes"},{"oid":"8","fname":"Haris","lname":"Okic"},{"oid":"9","fname":"Dino ","lname":"Okic"},{"oid":"10","fname":"Mirza","lname":"Cirkic"}]

当我运行我的应用程序时,我在创建序列化程序对象时或在 jsonserializer.ReadObject(stream) 行上得到一个 Invalid Casting 异常。关于为什么会发生这种情况的任何想法?

【问题讨论】:

    标签: c# json windows serialization windows-phone-7.1


    【解决方案1】:

    尝试用 int 指定属性:

    [DataMember(Name = "oid")]
    public string Oid { get; set; }
    

    应该是

    [DataMember(Name = "oid")]
    public int Oid { get; set; }
    

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多