2011-11-17 155 views
1

我想解析WP7上的复杂JSON。首先,我必须解析一个JSON提要,并检索数据来解析第二个JSON提要。解析复杂的JSON

为了解析我用这个Web服务的第一进 http://horaires-ter-sncf.naholyr.fr/prochainsdeparts.php?gare= ... 后,我们使用的代码和站点名称解析第二供给 http://horaires-ter-sncf.naholyr.fr/prochainsdeparts.php?gare=....&id= ...

这是我的代码,但它不'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); 
     } 
    } 
+1

什么不行?有什么异常? –

+1

显示无法反序列化的JSON和异常。 DataContractJsonSerializer存在一些您想要使用其他库的问题。 – invalidusername

+0

如果您在JSON.NET上使用了[RestSharp](http://restsharp.org/) –

回答

1

看看json.NET,它应该为您提供linq2json和一个简单的serialise对象。

+0

+1,那么您的代码总共可以更改为大约5行。 Json.net上的 –

+0

-1。是的,这是一个伟大的图书馆,毫无疑问。但是我在这个问题中没有看到任何暗示使用内置的DataContractJsonSerializer不能做的事情。为什么要依赖和分发另一个库? –

+1

我与@invalidusername对此,我们能否看到无法反序列化的JSON,以及在我们关闭使用另一个库之前关于该问题的更具体的问题,而不是“不起作用”? –

4

你的代码有几个问题。但即使您解决了这些问题,您也无法通过DataContractJsonSerializer开箱即解析收到的电台列表。

说明:

The web site offering the web service you are using说,从你的第一个 “JSON提要” 的回应是这样的:

{ “代码”:201, “成功”:{ “加雷斯”:{ “自由城d''alb.策”:1750年, “维尔 去rgue-12”:1749年,...}}}

看一看站及其ID:

{ “维尔-d''alb.-CE”:1750, “维尔德rgue-12”:1749,...}

这是一个关联数组,其中键和值没有用“钥匙”和“价值”明确装饰。但是这些装饰对于DataContractJsonSerializer解析JSON是必要的。他们将不得不在格式

[{“关键”:“自由城d''alb.-CE”,“值”:“1750”},{“关键”:“维尔 德rgue -12“,”Value“:”1749“}]

要被DataContractJsonSerializer正确解析。 (原因是,这串支持更复杂的类型比intstring用作键和值。)

This blog post包含的事很好的说明,以及如何JavaScriptSerializer可能是解决方案。但不幸的是,这个类在Silverlight中不可用。

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

解决方案:

使用Json.NET

+0

我和你在一起,需要一个“手动”解析。 datacontract不会削减它。 –