C# 无法从System.Int64强制转换或转换为System.Collections.Generic.List
当我尝试从TwitterAPI反序列化JSON响应时,我遇到以下异常。它有时会通过,但有时会有问题 以下是课程:C# 无法从System.Int64强制转换或转换为System.Collections.Generic.List,c#,json,casting,twitter,C#,Json,Casting,Twitter,当我尝试从TwitterAPI反序列化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
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; }
}
以下是详细错误:
我刚刚找到了答案 正如文档所述,问题在于Twitter API的局限性: 当应用程序超过给定API端点的速率限制时,Twitter API现在将返回HTTP 429错误 我超出了限制,所以它给出了一个例外 如果达到给定端点上的速率限制,您将看到HTTP 429消息的主体:
{
"errors": [
{
"code": 88,
"message": "Rate limit exceeded"
}
]
}
所以它只返回一个JSON格式的错误消息,并将其传递给反序列化函数,而反序列化函数又给出了该错误
谢谢大家的建议。你能发布twitterapi返回的sting吗?在调试器中运行,你会看到哪个属性不是你期望的(与你试图反序列化的JSON字符串相比)@EhsanUllah我根据不同的用户在一个循环中运行它。因此我有大约120个用户,因此它将为每个用户获取20条tweet…有几件事需要检查,您正在验证吗?文档中说,您需要为该操作提供支持。您是否也超过了查询限制?API还提到每15分钟有180条记录的限制。您似乎在循环中拉2400。@VishalSuthar如Adriano所述,在本例中,您必须对其进行调试,以确定按预期返回的属性。顺便说一下,试着把列表改成列表
https://api.twitter.com/1.1/statuses/user_timeline.json
{
"errors": [
{
"code": 88,
"message": "Rate limit exceeded"
}
]
}