1

我正在使用 Daniel Crenna 的 TweetSharp http://github.com/danielcrenna/tweetsharp

要将 TwitterStatus 序列化为文件,我使用代码

sw.WriteLine(JsonConvert.SerializeObject(twitterStatus));

产生文本

{"Id":288653019971727360,"InReplyToUserId":null,"InReplyToStatusId":null,"InReplyToScreenName":null,"truncated":false,"favorited":false,"Text":"Found the bug, dear old regular expressions","Source":"web","User":{"Id":1148081,"FollowersCount":793,"Name":"Tim Regan","Description": [...]

(注意,我不会全部包括在内,除非这有助于诊断?)

我反序列化 TwitterStatus 的代码行是

var tweet = (TwitterStatus)JsonConvert.DeserializeObject(line);

但这给出了错误

System.InvalidCastException was unhandled HResult=-2147467262 Message=Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'TweetSharp.TwitterStatus'. Source=TweetColorMVVM StackTrace: at TweetColorMVVM.Model.Tweets.LoadSavedTweets(String screenName) in c:\TFSCML\Users\Tim\MSR.Makefest\TwitterColor\TweetColorMVVM\Model\Tweets.cs:line 132 at TweetColorMVVM.Model.Tweets.LoadTweets(Object state) in c:\TFSCML\Users\Tim\MSR.Makefest\TwitterColor\TweetColorMVVM\Model\Tweets.cs:line 78 at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

谁能看到我做错了什么?

4

1 回答 1

1

我应该写:

var tweet = (TwitterStatus)JsonConvert.DeserializeObject(line, typeof(TwitterStatus));

现在可以了。

于 2013-01-08T22:43:37.987 回答