Parsing complex JSON

2019-07-24 23:10发布

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.

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);
        }
    }

2条回答
Evening l夕情丶
2楼-- · 2019-07-24 23:52

Have a look at json.NET, it should provide you with linq2json and a simple serialise to object.

查看更多
甜甜的少女心
3楼-- · 2019-07-25 00:07

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.

Explanation:

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

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

Have a look at the stations and their IDs:

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

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”}]

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.)

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:

Solution:

Use Json.NET.

查看更多
登录 后发表回答