2013-01-25 56 views
0

即時嘗試反序列化JSON數據Ive從facebook中檢索,但c#不會允許它,因爲「F.18-24」是一個無效變量,當我嘗試在公共課。我設法反序列化「度量」字段,但對於「F.18-24」字段,我不知道我該如何去做這件事。任何幫助,將不勝感激。這是否與Ext.data.Record有關,以使c#閱讀F.18-24?香港專業教育學院從Facebook獲取JSON數據在公共類中包含特殊字符,因此無效變量

我的JSON數據:

{"data":[ 
{ 
    "metric":"page_fans_gender_age", 
    "value": 
     {"F.18-24":19, 
     "M.18-24":14, 
     "M.25-34":4, 
     "F.13-17":1, 
     "M.13-17":1 
}}]} 

而我的代碼進行反序列化:

//geting isights data age & gender 
    private void kryptonButton1_Click_2(object sender, EventArgs e) 
    { 
     try 
     { 

      var client = new FacebookClient(myToken.Default.token); 
      var query = string.Format("SELECT metric, value FROM insights WHERE object_id=132414626916208 AND metric='page_fans_gender_age' AND period = period('lifetime') AND end_time = end_time_date('2013-01-18')"); 

      dynamic parameters = new ExpandoObject(); 
      parameters.q = query; 
      dynamic results = client.Get("/fql", parameters); 

      JavaScriptSerializer sr = new JavaScriptSerializer(); 
      String jsondata = String.Empty; 
      jsondata = results.ToString(); 

      genderAgeDataII converted = sr.Deserialize<genderAgeDataII>(jsondata); 
      Console.WriteLine(converted); 
     } 

     catch (Exception ex) { 
      MessageBox.Show(ex.Message); 
     } 

    } 


} 

//class for JSON Deserialize 
public class genderAgeDataII 
{ 
    public genderAgeData[] data { get; set; } 
} 

public class genderAgeData 
{ 
    public string metric { get; set; } 
    public genderAge value { get; set; } 
} 

public class genderAge 
{ 
    //public string numbers { get; set; } 
    /*string f1824 = "F.18-24"; 
    string m1824 = "M.18-24"; 
    string m2534 = "M.25-34"; 
    string f1317 = "F.13-17"; 
    string m1317 = "M.13-17"; */ 

    public int f1824 { get; set; } 
    public int m1824 { get; set; } 
    public int m2534 { get; set; } 
    public int f1317 { get; set; } 
    public int m1317 { get; set; } 
} 

回答

0

FB C#SDK支持泛型。您可以使用它作爲

var data = fb.Get<genderAgeDataII>("fql", parameters); 

https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/pull/203

然後使用數據契約和DataMember

[DataContract] 
public class genderAge 
{ 
    //public string numbers { get; set; } 
    /*string f1824 = "F.18-24"; 
    string m1824 = "M.18-24"; 
    string m2534 = "M.25-34"; 
    string f1317 = "F.13-17"; 
    string m1317 = "M.13-17"; */ 

    [DataMember(Name="F.18-24")] 
    public int f1824 { get; set; } 
    ..... 
} 
+0

天哪我是你燦爛的Facebook C#博客的粉絲!非常感謝你!!!這解決了我的問題! = d – SRA