0

当我尝试反序列化来自 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

下面是详细的错误: 错误

4

1 回答 1

0

我刚刚找到了答案。

正如文档所述,问题在于 Twitter API 的局限性:

“当应用程序超过给定 API 端点的速率限制时,Twitter API 现在将返回 HTTP 429 错误”

我超出了限制,所以它给出了一个例外(429) Too Many Requests

如果您达到给定端点的速率限制,您将看到以下 HTTP 429 消息的正文:

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

所以它只返回 JSON 格式的错误消息,并将其传递给反序列化函数,该函数反过来给出该错误。

谢谢大家的建议。

于 2013-07-06T05:40:04.753 回答