当我尝试从Twitter API反序列化JSON响应时,我收到以下异常。它有时会经历,但有时会有问题。
以下是课程:
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
以下是详细错误:
答案 0 :(得分:0)
我刚刚找到答案。
问题在于Twitter API的局限性,因为文档说:
“当应用程序超过给定API端点的速率限制时,Twitter API现在将返回HTTP 429错误”
我超出限制因此它提供了例外 (429) Too Many Requests 。
如果您达到给定端点的速率限制,这将是您将看到的HTTP 429消息的正文:
{
"errors": [
{
"code": 88,
"message": "Rate limit exceeded"
}
]
}
因此,它只返回JSON格式的错误消息,并将其传递给Deserialize函数,后者又发出错误。
感谢大家的建议。