2016-01-13 23 views
1

所以我已經成功解析了衆多API的數據,但是我似乎無法得到(一些)twitter的工作。解析來自Twitter的Json數據ASP.NET C# - 空數據,除非被解析成類型對象

我從端點「/1.1/statuses/user_timeline.json」

我有一個問題JSON數據拉動如下(見https://dev.twitter.com/overview/api/entities更多信息):

"entities":{ 
    "hashtags": [ 
     {"text":"myHasTag","indices":[24,53]} 
    ], 
    "symbols":[], 
    "user_mentions":[ 
     {"screen_name":"twitter","name":"Twitter","id":2353,"id_str":"2353","indices":[5,14]}, 
     {"screen_name":"TwitterDev","name":"TwitterDev","id":943434,"id_str":"943434","indices":[11,32]} 
    ], 
    "urls":[] 
} 

我想提取hastagsuser_mentions但當我使用JavaScriptSerializer解析數據時,它們都爲空。

在我的模型我有以下:

public partial class TwitterData_Entities 
{ 
    List<TwitterData_HashTag> hashtags { get; set; } 
    List<TwitterData_UserMentions> user_mentions { get; set; } 
} 
public partial class TwitterData_HashTag 
{ 
    public string text { get; set; } 
    public List<int> indices { get; set; } 
} 
public partial class TwitterData_UserMentions 
{ 
    public string screen_name { get; set; } 
    public string name { get; set; } 
    public long id { get; set; } 
    public List<int> indices { get; set; } 
} 

我還試圖加入下面的每個對象的行中TwitterData_Entities,但它並沒有區別

[JsonProperty(屬性名= 「user_mentions」)]

工作和填充的唯一事情是使用:

public partial class TwitterData_Entities 
{ 
    List<object> hashtags { get; set; } 
    List<object> user_mentions { get; set; } 
} 

的問題是,我不希望使用「對象」類型,它不會爲我的目的工作,它也是我的數據結構產生矛盾了。

你們建議如何解決這個問題?我無法在網上找到任何東西。

回答

1

我設法反序列化方式如下:

var json = "{" + 
      " \"entities\": {" + 
      " \"hashtags\": [" + 
      "  {" + 
      "  \"text\": \"myHasTag\"," + 
      "  \"indices\": [ 24, 53 ]" + 
      "  }" + 
      " ]," + 
      " \"symbols\": [ ]," + 
      " \"user_mentions\": [" + 
      "  {" + 
      "  \"screen_name\": \"twitter\"," + 
      "  \"name\": \"Twitter\"," + 
      "  \"id\": 2353," + 
      "  \"id_str\": \"2353\"," + 
      "  \"indices\": [ 5, 14 ]" + 
      "  }," + 
      "  {" + 
      "  \"screen_name\": \"TwitterDev\"," + 
      "  \"name\": \"TwitterDev\"," + 
      "  \"id\": 943434," + 
      "  \"id_str\": \"943434\"," + 
      "  \"indices\": [ 11, 32 ]" + 
      "  }" + 
      " ]," + 
      " \"urls\": [ ]" + 
      " }" + 
      "}"; 

var javascriptSerializer = new JavaScriptSerializer(); 
var deserialized = javascriptSerializer.Deserialize<TwitterData>(json); 

這只是您所提供嵌套在JSON {...}使它成爲一個有效的JSON和實際的反序列化。

我班有以下幾種:

public class TwitterData_Entities 
{ 
    public List<TwitterData_HashTag> hashtags { get; set; } 
    public List<TwitterData_UserMentions> user_mentions { get; set; } 
} 
public class TwitterData_HashTag 
{ 
    public string text { get; set; } 
    public List<int> indices { get; set; } 
} 
public class TwitterData_UserMentions 
{ 
    public string screen_name { get; set; } 
    public string name { get; set; } 
    public long id { get; set; } 
    public List<int> indices { get; set; } 
} 

public class TwitterData 
{ 
    public TwitterData_Entities entities { get; set; } 
} 

重要的是使用public訪問修飾符爲TwitterData_Entities類的屬性。

+0

謝謝你的回答。你是一個拯救生命的人。我會接受這個答案。 –