簡體   English   中英

無法將“X”類型的對象強制轉換為“X”

[英]Unable to cast object of type 'X' to type 'X'

我正在嘗試解析Windows Phone應用程序的JSON文件,我當前的代碼是:

 private void Button1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        Button1.FontSize = 15;
        Button1.Content = "Fetching...";
        var client = new WebClient();
        client.OpenReadCompleted +=
            (s, eargs) =>
            {
                var serializer = new DataContractJsonSerializer(typeof(RootObject));
                if (eargs.Error != null)
                {
                    if (eargs.Error.Message.Contains("NotFound"))
                    {
                        MessageBox.Show("Could not retrieve playlist", "Error", MessageBoxButton.OK);
                        Button1.Content = "Could not retrieve playlist";
                    }
                    else
                    {
                        MessageBox.Show("Could not retrieve playlist", "Error", MessageBoxButton.OK);
                        Button1.Content = "Could not retrieve playlist";
                    }
                }
                else
                {
                    var songHistory = (station1)serializer.ReadObject(eargs.Result);
                    Button1.Content = songHistory.text;
                }
            };
        var uri = new Uri("<JSONGOESHERE>");
        client.OpenReadAsync(uri);
    }

    public class station1
    {
        public string station { get; set; }
        public string title { get; set; }
        public string artist { get; set; }
        public string text { get; set; }
    }

    public class station2
    {
        public string station { get; set; }
        public int listeners { get; set; }
        public string title { get; set; }
        public string artist { get; set; }
        public string text { get; set; }
    }

    public class station3
    {
        public string station { get; set; }
        public int listeners { get; set; }
        public string title { get; set; }
        public string artist { get; set; }
        public string text { get; set; }
    }

    public class RootObject
    {
        public station1 station1 { get; set; }
        public station2 station2 { get; set; }
        public station3 station3 { get; set; }
    }

我后面也需要為“station2”和“station3”提取相同的文本,這就是為什么我把它們留在了。

目前,當我在線上運行時,會發生以下錯誤

var songHistory = (station1)serializer.ReadObject(eargs.Result);

用戶代碼未處理InvalidCastException類型'System.InvalidCastException'的異常在APP.DLL中發生但未在用戶代碼中處理無法將類型為'RootObject'的對象強制轉換為'station1'類型。

我曾使用json2csharp生成器來創建上面所以我不確定是什么問題。

任何幫助都是極好的。

您為RootObject類型創建了一個序列化RootObject 所以ReadObject的輸出就是那種類型。 只需正確轉換它( ReadObject沒有泛型重載):

var root = (RootObject)serializer.ReadObject(eargs.Result);
var songHistory = root.station1;

您確定該方法不必將Type作為反序列化的參數嗎?

var songHistory = (station1)serializer.ReadObject(eargs.Result, typeof(station1));

或者它可能有一個泛型重載進行反序列化並直接返回T?

var songHistory = serializer.ReadObject<station1>(eargs.Result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM