2013-07-05 574 views
0

当我尝试从Twitter API反序列化JSON响应时,出现以下异常。它有时会经历但有时会有问题。无法强制转换或从System.Int64转换为System.Collections.Generic.List

下面是类:

public static List<Tweet> Newtwt = new List<Tweet>(); 

public class Url2 
{ 
    public string url { get; set; } 
    public string expanded_url { get; set; } 
    public string display_url { get; set; } 
    public List<int> indices { get; set; } 
} 

public class Url 
{ 
    public List<Url2> urls { get; set; } 
} 

public class Description 
{ 
    public List<object> urls { get; set; } 
} 

public class Entities 
{ 
    public Url url { get; set; } 
    public Description description { get; set; } 
} 

public class User 
{ 
    public int id { get; set; } 
    public string id_str { get; set; } 
    public string name { get; set; } 
    public string screen_name { get; set; } 
    public string location { get; set; } 
    public string description { get; set; } 
    public string url { get; set; } 
    public Entities entities { get; set; } 
    public bool @protected { get; set; } 
    public int followers_count { get; set; } 
    public int friends_count { get; set; } 
    public int listed_count { get; set; } 
    public string created_at { get; set; } 
    public int favourites_count { get; set; } 
    public int utc_offset { get; set; } 
    public string time_zone { get; set; } 
    public bool geo_enabled { get; set; } 
    public bool verified { get; set; } 
    public int statuses_count { get; set; } 
    public string lang { get; set; } 
    public bool contributors_enabled { get; set; } 
    public bool is_translator { get; set; } 
    public string profile_background_color { get; set; } 
    public string profile_background_image_url { get; set; } 
    public string profile_background_image_url_https { get; set; } 
    public bool profile_background_tile { get; set; } 
    public string profile_image_url { get; set; } 
    public string profile_image_url_https { get; set; } 
    public string profile_link_color { get; set; } 
    public string profile_sidebar_border_color { get; set; } 
    public string profile_sidebar_fill_color { get; set; } 
    public string profile_text_color { get; set; } 
    public bool profile_use_background_image { get; set; } 
    public bool default_profile { get; set; } 
    public bool default_profile_image { get; set; } 
    public object following { get; set; } 
    public bool follow_request_sent { get; set; } 
    public object notifications { get; set; } 
} 

public class Entities2 
{ 
    public List<object> hashtags { get; set; } 
    public List<object> symbols { get; set; } 
    public List<object> urls { get; set; } 
    public List<object> user_mentions { get; set; } 
} 

public class RootObject 
{ 
    public string created_at { get; set; } 
    public object id { get; set; } 
    public string id_str { get; set; } 
    public string text { get; set; } 
    public string source { get; set; } 
    public bool truncated { get; set; } 
    public object in_reply_to_status_id { get; set; } 
    public object in_reply_to_status_id_str { get; set; } 
    public object in_reply_to_user_id { get; set; } 
    public object in_reply_to_user_id_str { get; set; } 
    public object in_reply_to_screen_name { get; set; } 
    public User user { get; set; } 
    public object geo { get; set; } 
    public object coordinates { get; set; } 
    public object place { get; set; } 
    public object contributors { get; set; } 
    public int retweet_count { get; set; } 
    public int favorite_count { get; set; } 
    public Entities2 entities { get; set; } 
    public bool favorited { get; set; } 
    public bool retweeted { get; set; } 
    public string lang { get; set; } 
    public bool? possibly_sensitive { get; set; } 
} 

public class Tweet 
{ 
    public string UserName { get; set; } 
    [JsonProperty(PropertyName = "text")] 
    public string Message { get; set; } 
    [JsonProperty(PropertyName = "id")] 
    public object ID { get; set; } 
    [JsonProperty(PropertyName = "created_at")] 
    public DateTime TwitTime { get; set; } 
} 

我尝试反序列化下列方式响应:

string str = TwitterAPI(Request.QueryString["screen_name"].ToString(), "10"); 
var root = JsonConvert.DeserializeObject<List<RootObject>>(str); 

TwitterAPI()将返回来自跟随Twitter的API响应:

https://api.twitter.com/1.1/statuses/user_timeline.json 

下面是详细的错误: Error

+1

可以请您发布twitterapi正在返回的刺痛吗? – Ehsan

+0

在调试器**内运行**,您会看到哪个**属性**不是您所期望的(与** JSON字符串**相比,您试图反序列化)。 –

+0

@EhsanUllah我在一个基于不同用户的循环中运行它..因此,我有大约120个用户,因此它将为每个用户提取20条推文...... –

回答

0

我刚刚找到答案。

的问题是与Twitter API的限制,文件说:

"When an application exceeds the rate limit for a given API endpoint, the Twitter API will now return an HTTP 429 error"

,我是超过了上限,从而使其被赋予一个异常(429) Too Many Requests

如果碰到一个给定的端点速率限制,这是HTTP 429消息的身体,你会看到:

{ 
    "errors": [ 
    { 
     "code": 88, 
     "message": "Rate limit exceeded" 
    } 
    ] 
} 

于是有人以JSON格式只返回一个错误信息,这是将它传递给反序列化函数,反过来给出这个错误。

谢谢大家的建议。

相关问题