解析复杂的JSON [英] Parsing complex JSON

查看:108
本文介绍了解析复杂的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WP7上解析复杂的JSON.首先,我必须解析JSON提要,然后检索数据以解析第二个JSON提要.

I want to parse a complex JSON on WP7. First I have to parse a JSON feed and I retrieve data to parse second JSON feed.

要解析第一个提要,我使用了此Web服务 http://horaires-ter-sncf.naholyr.fr/prochainsdeparts.php? gare = ... 之后,我们使用代码和站点名称来解析第二个提要 http://horaires-ter-sncf. naholyr.fr/prochainsdeparts.php?gare=....&id= ...

To parse the first feed I use this web service http://horaires-ter-sncf.naholyr.fr/prochainsdeparts.php?gare=... after that we use the code and the station's name to parse the second feed http://horaires-ter-sncf.naholyr.fr/prochainsdeparts.php?gare=....&id=...

这是我的代码,但是不起作用:

This my code but it doesn't work:

public static class Parser
        {
            public static string resultats;
            public static reponse[] obj = new reponse[]{new reponse(),new reponse()};
            public static reponse1 obj1 = new reponse1();


            public static void HttpsCompleted_reponse(object sender, DownloadStringCompletedEventArgs e)
            {
                Horaire hre =new Horaire();
                  try
                {
                    var ms = new MemoryStream(Encoding.Unicode.GetBytes(resultats));
                    var ser = new DataContractJsonSerializer(typeof(reponse1));
                    obj1 = (reponse1)ser.ReadObject(ms);

                }

                catch
                {

                    WebClient wc = new WebClient();
                    //wc.DownloadStringCompleted += HttpsCompleted_reponse1;
                    wc.DownloadStringAsync(new Uri("http://horaires-ter-sncf.naholyr.fr/prochainsdeparts.php?gare=" +hre.gettxt()));
                    Debug.WriteLine("youuuuuuuuuuuuuuuuuuuuuuuppppppppppiiii");
                }

            }
            /*
            public static void HttpsCompleted_reponse1(object sender, DownloadStringCompletedEventArgs e)
            {
                try
                {
                    var ms = new MemoryStream(Encoding.Unicode.GetBytes(resultats));
                    var ser = new DataContractJsonSerializer(typeof(Gare));
                    obj1 = (reponse1)ser.ReadObject(ms);

                }

                catch
                {

                    WebClient wc = new WebClient();
                        wc.DownloadStringCompleted += HttpsCompleted_reponse;
                        wc.DownloadStringAsync(new Uri("http://horaires-ter-sncf.naholyr.fr/prochainsdeparts.php?gare=" + obj.success.Gares.Eleme + "&id=" + obj.success.id));

                }

            }

        */

        }
        public class Depart
        {
            [DataMember(Name = "type")]
            public string type { get; set; }
            [DataMember(Name = "heure")]
            public string heure { get; set; }
            [DataMember(Name = "destination")]
            public string destination { get; set; }
            [DataMember(Name="attention")]
            public string attention { get; set; }
            [DataMember(Name = "retards")]
            public string [] retards { get; set; }
            [DataMember(Name = "source")]
            public string source { get; set; }
            public Depart()
            {
            }
        }

        public class Success {
            [DataMember(Name = "nom")]
            public string nom { get; set; }
            [DataMember(Name = "id")]
            public int id { get; set; }
            [DataMember(Name = "departs")]
            public Depart[] departs { get; set; }
                    public Success() 
                    {
                        this.departs = new Depart[0];
                    }
             }
        public class Success1
        {
            [DataMember(Name="Gares")]
            public Gare[] Gares { get; set; }
                public Success1()
                {
                    this.Gares = new Gare[0];
                }
        }
        public class Gare{


            [DataMember(Name="Nom")]
            public string Nom { get; set; }
            [DataMember(Name="code")]
            public int code { get; set; }
                public Gare() 
                {
                }
        }

        public class reponse
        {

            [DataMember(Name = "code")]
            public string code{get;set;}
            [DataMember(Name = "success")]
            public Success1 success{get;set;}
                public reponse()
                {
                    this.success = new Success1();
                }
             }
        public class reponse1 {
            [DataMember(Name = "code")]
            public string code { get; set; }
            [DataMember(Name = "success")]
            public Success success { get; set; }
                public reponse1() 
                {
                    this.success = new Success();
                }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //for (int i=0; i<=Parser.obj1.Length;i++)
            Debug.WriteLine(Parser.obj1.success.nom);
        }
    }

推荐答案

您的代码中有几个问题.但是,即使您解决了这些问题,也无法使用DataContractJsonSerializer来解析接收到的电台列表.

There are several problems in your code. But even if you solved them you wouldn't be able to parse the list of received stations with DataContractJsonSerializer out of the box.

说明:

提供您正在使用的Web服务的网站表示您的第一个"JSON提要"的响应如下所示:

The web site offering the web service you are using says a response from your first "JSON feed" looks like this:

{代码":201,成功":{"gares":{"villefranche-d" alb.-ce":1750,"villefranche de rgue-12:1749,...}}}

{"code":201,"success":{"gares":{"villefranche-d''alb.-ce":1750,"villefranche de rgue-12":1749,...}}}

看看这些电台及其ID:

Have a look at the stations and their IDs:

{"villefranche-d" alb.-ce:1750," villefranche de rgue-12:1749,...}

{"villefranche-d''alb.-ce":1750,"villefranche de rgue-12":1749,...}

这是一个关联数组,其中键和值未明确用键"和值"修饰.但是,这些装饰对于DataContractJsonSerializer解析JSON是必需的.它们必须采用

This is an associative array where the keys and values are not explicitly decorated with "Key" and "Value". But these decorations are necessary for DataContractJsonSerializer to parse the JSON. They would have to be in format

[{"Key":"villefranche-d" alb.-ce," Value:" 1750},{" Key:" villefranche de rgue-12","Value":"1749"}]

[{"Key":"villefranche-d''alb.-ce","Value":"1750"},{"Key":"villefranche de rgue-12","Value":"1749"}]

可以由DataContractJsonSerializer正确解析. (原因是此序列化程序支持比intstring用作键和值的类型更复杂的类型.)

to be properly parsed by DataContractJsonSerializer. (The reason is that this serializer supports more complex types than int and string to be used as keys and values.)

此博客文章包含非常好的描述问题以及JavaScriptSerializer如何解决.但是不幸的是,该类在Silverlight中不可用.

This blog post contains a very good description of the matter and how JavaScriptSerializer could be the solution. But unfortunately this class isn't available in Silverlight.

更多有类似问题的人像你一样

More people having similar problems like you:

  • Deserialization problem with DataContractJsonSerializer
  • .NET: Can I use DataContractJsonSerializer to serialize to a JSON associative array?
  • https://json.codeplex.com/discussions/258083
  • https://connect.microsoft.com/VisualStudio/feedback/details/558686/

解决方案:

使用 Json.NET .

这篇关于解析复杂的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆