2012-03-06 42 views
1

我正在C#中使用我自己的小型Twitter應用程序。 我已經成功地從序列https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name= 它返回這樣的JSON數據:C#序列化JSON從Twitter沒有數據成員

{ "ids" : [ 401295021, 
    506271294, 
    14405250, 
    25873220 
], 
"next_cursor" : 0, 
"next_cursor_str" : "0", 
"previous_cursor" : 0, 
"previous_cursor_str" : "0" 
} 

使用這個類,我可以把它序列化:

[DataContract] 
    public class TwitterFollowers 
    { 
     [DataMember(Name = "ids")] 
     public IList<int> AccountIDs { get; set; } 
    } 

現在我想要得到的屏幕名稱追隨者,所以我用這個網址: http://api.twitter.com/1/users/lookup.json?user_id=

此JSON看起來是這樣的:

[ { "contributors_enabled" : false, 
"created_at" : "Wed Apr 16 06:30:52 +0000 2008", 
"default_profile" : false, 
"default_profile_image" : false, 
"description" : "", 
"utc_offset" : -25200, 
"verified" : false 
}, 
{ "contributors_enabled" : false, 
"created_at" : "Tue Mar 04 12:31:57 +0000 2008", 
"default_profile" : true, 
"default_profile_image" : false, 
"description" : "", 
"utc_offset" : 3600, 
"verified" : false 
} 
] 

正如你所看到的,該陣列立即啓動,而無需命名。 我的班級應該如何序列化這個?

我試過這個,但是,這並不工作:

[DataContract] 
    public class TwitterProfiles 
    { 
     [DataMember(Name = "")] 
     public IList<TwitterProfile> Profiles { get; set; } 
    } 

    [DataContract] 
    public class TwitterProfile 
    { 
     [DataMember(Name = "lang")] 
     public string Language { get; set; } 

     [DataMember(Name = "location")] 
     public string Location { get; set; } 

     [DataMember(Name = "name")] 
     public string Name { get; set; } 

     [DataMember(Name = "screen_name")] 
     public string ScreenName { get; set; } 

     [DataMember(Name = "url")] 
     public string URL { get; set; } 
    } 
+0

您是否嘗試過只刪除「名稱」屬性可將DataMember屬性一起?含義,[DataMember] public IList Profiles {get;組; } – 2012-03-06 22:11:56

+0

你的意思是反序列化? – Kekoa 2012-03-06 22:26:18

回答

0

您是否嘗試過刪除「名稱」屬性關的數據成員屬性一起?含義:

[DataMember] 
public IList<TwitterProfile> Profiles { get; set; } 

如果不工作,一個選項你也可以做的是抓住從API調用的輸出,並格式化成你知道你可以反序列化JSON字符串:

string.Format("{{\"profiles\":{0}}}", GetJsonResponse()) 
+0

將配置文件添加到json響應中是有用的。 – 2012-03-06 22:40:50

0

您提供的樣本與類TwitterProfile的成員不匹配。儘管如此,你不需要一個輔助類來處理數組。您可以直接在反序列化中直接定位數組類型。

以下是可用於測試的代碼示例。我已將您的TwitterProfiles課程刪除,並將TwitterProfile拆除爲只有名爲CreatedAt的字段。

注意,在下面的源以下行:

DataContractJsonSerializer js = 
    new DataContractJsonSerializer(typeof(TwitterProfile[])); 

這裏是休息。

using System.Windows.Forms; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Json; 
using System.IO; 

namespace TwProfileJson 
{ 
    class Program 
    { 
     [STAThread] 
     static void Main(string[] args) 
     { 
      OpenFileDialog dlg = new OpenFileDialog(); 
      if (dlg.ShowDialog() != DialogResult.OK) { return; } 
      string json = System.IO.File.ReadAllText(dlg.FileName); 

      using(MemoryStream stm = new MemoryStream(Encoding.UTF8.GetBytes(json))) 
      { 
       DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(TwitterProfile[])); 
       TwitterProfile[] pps = (TwitterProfile[])js.ReadObject(stm); 

       foreach(TwitterProfile p in pps) 
       { 
        Console.WriteLine(p.CreatedAt); 
       } 
      } 

      Console.ReadKey(); 
     } 

     [DataContract] 
     public class TwitterProfile 
     { 
      [DataMember(Name = "created_at")] 
      public string CreatedAt { get; set; } 
     } 
    } 
}