1

我有一个包含以 JSON 格式存储的推文的文本文件。

"{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"I find wrapping myself up in my rug more fun than I should.\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954832220160\",\"favorited\":false,\"geo\":null,\"source\":\"\u003Ca href=\\"http:\/\/blackberry.com\/twitter\\" rel=\\"nofollow\\"\u003ETwitter for BlackBerry\u00ae\u003C\/a\u003E\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":true,\"lang\":\"en\",\"friends_count\":200,\"profile_sidebar_border_color\":\"BDDCAD\",\"location\":\"Newcastle\",\"profile_background_image_url_https\":\"https:\/\/si0.twimg.com\/profile_background_images\/613341681\/ed47iwy7apcu7nltbj1y.jpeg\",\"id_str\":\"369637639\",\"listed_count\":0,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\/\/si0.twimg.com\/profile_images\/2459865521\/photo_653_normal.jpg\",\"description\":\"Hi, I'm Ellen, but you've probably guessed that by now. You can follow me but I don't know where I'm going.\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"333333\",\"default_profile\":false,\"profile_background_image_url\":\"http:\/\/a0.twimg.com\/profile_background_images\/613341681\/ed47iwy7apcu7nltbj1y.jpeg\",\"followers_count\":335,\"is_translator\":false,\"time_zone\":\"London\",\"profile_link_color\":\"0084B4\",\"protected\":false,\"created_at\":\"Wed Sep 07 17:56:39 +0000 2011\",\"profile_background_color\":\"9AE4E8\",\"name\":\"ellen\u2020urnbull \",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":788,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":false,\"url\":null,\"profile_image_url\":\"http:\/\/a0.twimg.com\/profile_images\/2459865521\/photo_653_normal.jpg\",\"screen_name\":\"ellenteee\",\"id\":369637639,\"verified\":false,\"utc_offset\":0,\"favourites_count\":10,\"profile_sidebar_fill_color\":\"DDFFCC\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954832220160}
"
"{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"Goodevening!\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954828029952\",\"favorited\":false,\"geo\":null,\"source\":\"\u003Ca href=\\"http:\/\/twitter.com\/download\/android\\" rel=\\"nofollow\\"\u003ETwitter for Android\u003C\/a\u003E\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":true,\"lang\":\"en\",\"friends_count\":130,\"profile_sidebar_border_color\":\"D3D2CF\",\"location\":\"#3block,St. Rose \",\"profile_background_image_url_https\":\"https:\/\/si0.twimg.com\/profile_background_images\/341819936\/Tyga.jpg\",\"id_str\":\"331869167\",\"listed_count\":0,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\/\/si0.twimg.com\/sticky\/default_profile_images\/default_profile_0_normal.png\",\"description\":\" R.I.P Mealey you will be truly miss. Gone but never forgotten. #teamGod #HeatNation\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"634047\",\"default_profile\":false,\"profile_background_image_url\":\"http:\/\/a0.twimg.com\/profile_background_images\/341819936\/Tyga.jpg\",\"followers_count\":100,\"is_translator\":false,\"time_zone\":\"Central Time (US & Canada)\",\"profile_link_color\":\"088253\",\"protected\":false,\"created_at\":\"Fri Jul 08 21:06:02 +0000 2011\",\"profile_background_color\":\"EDECE9\",\"name\":\"T\u00e9 lu\u00f2 y\u012b \",\"default_profile_image\":true,\"contributors_enabled\":false,\"statuses_count\":16944,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":false,\"url\":null,\"profile_image_url\":\"http:\/\/a0.twimg.com\/sticky\/default_profile_images\/default_profile_0_normal.png\",\"screen_name\":\"Troy_rollupone\",\"id\":331869167,\"verified\":false,\"utc_offset\":-21600,\"favourites_count\":41,\"profile_sidebar_fill_color\":\"E3E2DE\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954828029952}
"

我使用以下方法扫描文件:

> twit <- scan("twitCapture2.txt","",sep="\n")
Read 4 items
> twit
[1] "\"{\\\"contributors\\\":null,\\\"coordinates\\\":null,\\\"in_reply_to_user_id\\\":null,\\\"truncated\\\":false,\\\"text\\\":\\\"I find wrapping myself up in my rug more fun than I should.\\\",\\\"entities\\\":{\\\"hashtags\\\":[],\\\"urls\\\":[],\\\"user_mentions\\\":[]},\\\"place\\\":null,\\\"id_str\\\":\\\"234702954832220160\\\",\\\"favorited\\\":false,\\\"geo\\\":null,\\\"source\\\":\\\"\\u003Ca href=\\\\\"http:\\/\\/blackberry.com\\/twitter\\\\\" rel=\\\\\"nofollow\\\\\"\\u003ETwitter for BlackBerry\\u00ae\\u003C\\/a\\u003E\\\",\\\"retweet_count\\\":0,\\\"in_reply_to_status_id_str\\\":null,\\\"in_reply_to_screen_name\\\":null,\\\"created_at\\\":\\\"Sun Aug 12 17:28:39 +0000 2012\\\",\\\"in_reply_to_user_id_str\\\":null,\\\"user\\\":{\\\"show_all_inline_media\\\":true,\\\"lang\\\":\\\"en\\\",\\\"friends_count\\\":200,\\\"profile_sidebar_border_color\\\":\\\"BDDCAD\\\",\\\"location\\\":\\\"Newcastle\\\",\\\"profile_background_image_url_https\\\":\\\"https:\\/\\/si0.twimg.com\\/profile_background_images\\/613341681\\/ed47iwy7apcu7nltbj1y.jpeg\\\",\\\"id_str\\\":\\\"369637639\\\",\\\"listed_count\\\":0,\\\"profile_use_background_image\\\":true,\\\"profile_image_url_https\\\":\\\"https:\\/\\/si0.twimg.com\\/profile_images\\/2459865521\\/photo_653_normal.jpg\\\",\\\"description\\\":\\\"Hi, I'm Ellen, but you've probably guessed that by now. You can follow me but I don't know where I'm going.\\\",\\\"follow_request_sent\\\":null,\\\"following\\\":null,\\\"profile_text_color\\\":\\\"333333\\\",\\\"default_profile\\\":false,\\\"profile_background_image_url\\\":\\\"http:\\/\\/a0.twimg.com\\/profile_background_images\\/613341681\\/ed47iwy7apcu7nltbj1y.jpeg\\\",\\\"followers_count\\\":335,\\\"is_translator\\\":false,\\\"time_zone\\\":\\\"London\\\",\\\"profile_link_color\\\":\\\"0084B4\\\",\\\"protected\\\":false,\\\"created_at\\\":\\\"Wed Sep 07 17:56:39 +0000 2011\\\",\\\"profile_background_color\\\":\\\"9AE4E8\\\",\\\"name\\\":\\\"ellen\\u2020urnbull \\\",\\\"default_profile_image\\\":false,\\\"contributors_enabled\\\":false,\\\"statuses_count\\\":788,\\\"geo_enabled\\\":false,\\\"notifications\\\":null,\\\"profile_background_tile\\\":false,\\\"url\\\":null,\\\"profile_image_url\\\":\\\"http:\\/\\/a0.twimg.com\\/profile_images\\/2459865521\\/photo_653_normal.jpg\\\",\\\"screen_name\\\":\\\"ellenteee\\\",\\\"id\\\":369637639,\\\"verified\\\":false,\\\"utc_offset\\\":0,\\\"favourites_count\\\":10,\\\"profile_sidebar_fill_color\\\":\\\"DDFFCC\\\"},\\\"retweeted\\\":false,\\\"in_reply_to_status_id\\\":null,\\\"id\\\":234702954832220160}"
[2] "\""                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
[3] "\"{\\\"contributors\\\":null,\\\"coordinates\\\":null,\\\"in_reply_to_user_id\\\":null,\\\"truncated\\\":false,\\\"text\\\":\\\"Goodevening!\\\",\\\"entities\\\":{\\\"hashtags\\\":[],\\\"urls\\\":[],\\\"user_mentions\\\":[]},\\\"place\\\":null,\\\"id_str\\\":\\\"234702954828029952\\\",\\\"favorited\\\":false,\\\"geo\\\":null,\\\"source\\\":\\\"\\u003Ca href=\\\\\"http:\\/\\/twitter.com\\/download\\/android\\\\\" rel=\\\\\"nofollow\\\\\"\\u003ETwitter for Android\\u003C\\/a\\u003E\\\",\\\"retweet_count\\\":0,\\\"in_reply_to_status_id_str\\\":null,\\\"in_reply_to_screen_name\\\":null,\\\"created_at\\\":\\\"Sun Aug 12 17:28:39 +0000 2012\\\",\\\"in_reply_to_user_id_str\\\":null,\\\"user\\\":{\\\"show_all_inline_media\\\":true,\\\"lang\\\":\\\"en\\\",\\\"friends_count\\\":130,\\\"profile_sidebar_border_color\\\":\\\"D3D2CF\\\",\\\"location\\\":\\\"#3block,St. Rose \\\",\\\"profile_background_image_url_https\\\":\\\"https:\\/\\/si0.twimg.com\\/profile_background_images\\/341819936\\/Tyga.jpg\\\",\\\"id_str\\\":\\\"331869167\\\",\\\"listed_count\\\":0,\\\"profile_use_background_image\\\":true,\\\"profile_image_url_https\\\":\\\"https:\\/\\/si0.twimg.com\\/sticky\\/default_profile_images\\/default_profile_0_normal.png\\\",\\\"description\\\":\\\" R.I.P Mealey you will be truly miss. Gone but never forgotten. #teamGod #HeatNation\\\",\\\"follow_request_sent\\\":null,\\\"following\\\":null,\\\"profile_text_color\\\":\\\"634047\\\",\\\"default_profile\\\":false,\\\"profile_background_image_url\\\":\\\"http:\\/\\/a0.twimg.com\\/profile_background_images\\/341819936\\/Tyga.jpg\\\",\\\"followers_count\\\":100,\\\"is_translator\\\":false,\\\"time_zone\\\":\\\"Central Time (US & Canada)\\\",\\\"profile_link_color\\\":\\\"088253\\\",\\\"protected\\\":false,\\\"created_at\\\":\\\"Fri Jul 08 21:06:02 +0000 2011\\\",\\\"profile_background_color\\\":\\\"EDECE9\\\",\\\"name\\\":\\\"T\\u00e9 lu\\u00f2 y\\u012b \\\",\\\"default_profile_image\\\":true,\\\"contributors_enabled\\\":false,\\\"statuses_count\\\":16944,\\\"geo_enabled\\\":false,\\\"notifications\\\":null,\\\"profile_background_tile\\\":false,\\\"url\\\":null,\\\"profile_image_url\\\":\\\"http:\\/\\/a0.twimg.com\\/sticky\\/default_profile_images\\/default_profile_0_normal.png\\\",\\\"screen_name\\\":\\\"Troy_rollupone\\\",\\\"id\\\":331869167,\\\"verified\\\":false,\\\"utc_offset\\\":-21600,\\\"favourites_count\\\":41,\\\"profile_sidebar_fill_color\\\":\\\"E3E2DE\\\"},\\\"retweeted\\\":false,\\\"in_reply_to_status_id\\\":null,\\\"id\\\":234702954828029952}"                                   
[4] "\""                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
> 

然后,当我尝试使用时fromJSON,出现以下错误:

> library(RJSONIO)
> 
> twit1<-fromJSON(twit1)
Error in file(con, "r") : invalid 'description' argument
> twit1<-fromJSON(twit1[1])
Error in file(con, "r") : cannot open the connection

> f <- RJSONIO::fromJSON(toJSON(twit1[3]))
> f$text
Error in f$text : $ operator is invalid for atomic vectors
> f <- rjson::fromJSON(toJSON(twit1[3]))
> f$text
Error in f$text : $ operator is invalid for atomic vectors
> 

关于我做错了什么的任何想法?

4

4 回答 4

2

“$ 运算符对原子向量无效”似乎是由以下原因引起的:

不工作: {a: 1, b: 2} {a: 3, b: 4, c: 5}

工作正常: {results: [ {a: 1, b: 2}, {a: 3, b: 4, c: 5} ], status: 1}

(请注意,您需要说 data$results$a 而不仅仅是 data$a)

于 2012-11-08T20:16:16.093 回答
1

你有没有尝试过

twit1<-fromJSON(twit[1])
于 2012-08-21T13:01:11.757 回答
1

看起来您的 json 无效。有很多转义字符。它从哪里来的?您可能可以摆脱所有这些转义斜线。

于 2012-08-21T13:14:56.090 回答
0

我在从中检索对象时遇到了类似的问题window.localStorage

希望这可以找到使用本地存储的类似问题的人


我的问题:

localStorage.myVariable = undefined;

解决方案:

localStorage.removeItem("myVariable");

解释:

即使我从未使用引号,它也会将 localStorage.myVariable 转换为 STRING“未定义”!


我的应用程序如何导致斜线:

这引起了一大堆斜线,因为我有一个字符串不断被字符串化!哎呀

var stored = localStorage.myVariable ? JSON.parse(localStorage.myVariable) : {};

我的三元 if/else 检查理解 localStorage.myVariable 是真实的,因为它是一个 STRING 而不是 UNDEFINED

我会扩展stored变量(我认为它是一个对象),然后使用重新编码localStorage.myVariable = JSON.stringify(stored); // BAD -- stringifying a string

于 2014-08-15T23:16:18.567 回答