1

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);
        }
    }
Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
Wafa Gheni
  • 11
  • 3

2 Answers2

4

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.

Community
  • 1
  • 1
Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
1

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

Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84
  • -1 on Json.net. Yes, it is a great library, without question. But I didn't see anything in this question that suggests that using the built-in DataContractJsonSerializer would not do. Why take a dependency on, and distribute, another library? – Visual Stuart Nov 17 '11 at 19:50
  • 1
    I'm with @invalidusername on this, can we see the JSON that is failing to deserialize, and something more specific about the problem than "it doesn't work" before we charge off to use another library? – Visual Stuart Nov 17 '11 at 20:06
  • If the JSON is mixing key value pairs and then randomly switching to a list then the best way to parse it is to use something like linq2json to parse it manually. badly formed json or xml doesn't parse too well with data contract serialiser. – Joseph Le Brech Nov 18 '11 at 10:10