2013-01-08 33 views
1

我使用的是丹尼爾Crenna的TweetSharp http://github.com/danielcrenna/tweetsharp反序列化JSON TweetSharp TwitterStatus

要連載一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": [...]

( NB我不會把它全部包括在內,除非這有助於診斷?)

我的代碼反序列化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()

任何人都可以看到我有錯嗎?

回答

1

我應該寫:

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

它現在。