简体   繁体   English

无法将“X”类型的对象强制转换为“X”

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

I'm trying to parse a JSON file for a Windows Phone app, and my current code is: 我正在尝试解析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; }
    }

I require the same fetching of text for "station2" and "station3" later as well, which is why I've left them in. 我后面也需要为“station2”和“station3”提取相同的文本,这就是为什么我把它们留在了。

Currently, what happens is I am getting the following error when I run this on the line 目前,当我在线上运行时,会发生以下错误

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

InvalidCastException was unhandled by user code An exception of type 'System.InvalidCastException' occurred in APP.DLL but was not handled in user code Unable to cast object of type 'RootObject' to type 'station1'. 用户代码未处理InvalidCastException类型'System.InvalidCastException'的异常在APP.DLL中发生但未在用户代码中处理无法将类型为'RootObject'的对象强制转换为'station1'类型。

I had used the json2csharp generator to create the above so I'm not sure what's wrong. 我曾使用json2csharp生成器来创建上面所以我不确定是什么问题。

Any help would be awesome. 任何帮助都是极好的。

You created a serializer for the type RootObject . 您为RootObject类型创建了一个序列化RootObject So the output of ReadObject is that type. 所以ReadObject的输出就是那种类型。 Just cast it correctly (there is no generic overload for ReadObject ): 只需正确转换它( ReadObject没有泛型重载):

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

Are you sure the method doesn't also have to take a Type as a parameter for deserialization? 您确定该方法不必将Type作为反序列化的参数吗?

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

Or maybe it has a generic overload that does the deserialization and returns a T directly? 或者它可能有一个泛型重载进行反序列化并直接返回T?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM