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

时间:2013-01-03 08:33:12

标签: c# json windows-phone-7 parsing windows-phone-8

我正在尝试解析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未被用户代码处理 APP.DLL中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理 无法将“RootObject”类型的对象强制转换为“station1”。

我曾使用json2csharp生成器来创建上面的内容,所以我不确定是什么问题。

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:2)

您为类型RootObject创建了序列化程序。所以ReadObject的输出就是那种类型。只需正确转换它(ReadObject没有通用重载):

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

答案 1 :(得分:0)

您确定该方法还不必将Type作为反序列化的参数吗?

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

或许它有一个通用的重载来进行反序列化并直接返回一个T?

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