无效的Cast Exception,json,windows phone

时间:2012-04-02 10:50:03

标签: json windows-phone-7

嗨,为什么我会得到一个愚蠢的演员异常?

    public class RootContainer2
    {
            [DataMember]
            public string StopName { get; set; }

            [DataMember]
            public string StopId { get; set; }

            [DataMember]
            public string Stop { get; set; }

            [DataMember]
            public ObservableCollection<Stops> Stops { get; set; }
    }

        private void ContentPanel_Loaded(object sender, RoutedEventArgs e)
        {
            textRouteId.Text = this.NavigationContext.QueryString["name"];

            string myvar = textRouteId.Text;

            try
            {
                WebClient webClient = new WebClient();
                Uri uri = new Uri("websiteurl");
                webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
                webClient.OpenReadAsync(uri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            DataContractJsonSerializer ser = null;

            try
            {
                ser = new DataContractJsonSerializer(typeof(RootContainer2));
                RootContainer2 rootContainer = ser.ReadObject(e.Result) as RootContainer2;
                foreach (Stops em in rootContainer.Stops)
                {

                    string df = em.StopName;
                    string dt = em.StopId;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

这是我想要阅读的json输出,但是我得到了一个无效的强制转换异常。

{"RouteId": "1231", "RouteName":"1 Whitehawk - Mile Oak", "Stops": [ { "StopId":"6882", "StopName":"Swanborough Drive", "OperatorsCode4":"bridmpj", "Lat":"50.8326729229821", "Lng":"-0.103217996656901" } , { "StopId":"6883", "StopName":"Haybourne Road", "OperatorsCode4":"brimapm", "Lat":"50.8317788816436", "Lng":"-0.10486427645364671" } , { "StopId":"6884", "StopName":"Coolham Drive", "OperatorsCode4":"brijtwm", "Lat":"50.829695439856089", "Lng":"-0.10512229365031489" } , { "StopId":"6885", "StopName":"St Cuthmans Church", "OperatorsCode4":"briapdg", "Lat":"50.8283233642578", "Lng":"-0.104808002710342" } , 

加上stops.cs中的类:

public class Stops
{
    public string StopId { get; set; }
    public string StopName { get; set; }
}

编辑:

因此,在创建自己的网址然后测试数据后,这与我认为给出的URL有关。只是做进一步的测试。

编辑2

如果我将数据放入文本文件并调试数据,如果我执行以下操作,则可以正常工作。

Feed的结尾是这样的:

“Lng”:“ - 0.277833998203278”,“”}}}

如果我删除了,和“”那么它的工作原理。但是我如何阻止它首先到达那里?

2 个答案:

答案 0 :(得分:0)

ReadObject()接受Stream(或一些与XML相关的类型),但e.Result是object类型。

尝试:

RootContainer2 rootContainer = ser.ReadObject((Stream)e.Result) as RootContainer2;

编辑:第二个想法,这可能会引发无效参数异常,而不是无效的转换。

ReadObject()返回的对象看起来无法转换为RootContainer2类型。

答案 1 :(得分:0)

异常可能是由于缺少[DataContract][DataMember]属性造成的,尤其是Stops类。除了Silverlight 5之外,DataContract是一个选择加入模型,其中应该序列化的所有类和属性都必须带有显式属性。

<强>更新

在您发表评论之后,我会更详细地了解您收到的JSON数据。你提供两件JSON。如果合并,它们看起来像这样:

{
    "RouteId": "1231",
    "RouteName": "1 Whitehawk - Mile Oak",
    "Stops": [
        {
            "StopId": "6882",
            "StopName": "Swanborough Drive",
            "OperatorsCode4": "bridmpj",
            "Lat": "50.8326729229821",
            "Lng": "-0.103217996656901"
        },
        {
            "StopId": "6883",
            "StopName": "Haybourne Road",
            "OperatorsCode4": "brimapm",
            "Lat": "50.8317788816436",
            "Lng": "-0.10486427645364671"
        },
        {
            "StopId": "6884",
            "StopName": "Coolham Drive",
            "OperatorsCode4": "brijtwm",
            "Lat": "50.829695439856089",
            "Lng": "-0.10512229365031489"
        },
        {
            "StopId": "6885",
            "StopName": "St Cuthmans Church",
            "OperatorsCode4": "briapdg",
            "Lat": "50.8283233642578",
            "Lng": "-0.277833998203278",
            ""
        }
    ]
}

我担心这对JSON来说无效。当你发现自己有一个额外的逗号和一对额外的emtpy引号接近结尾。它们区分正确和无效的JSON。您可以使用JSONLint等onlne JSON验证程序进行检查。

最好的解决方案是,如果您可以在服务器端修复它。否则,最好在将数据传递给序列化程序之前尝试修复数据。