【问题标题】:R / JSON Can't figure out parsing issueR / JSON无法解决解析问题
【发布时间】:2014-08-16 07:16:18
【问题描述】:

我有一个包含以 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
> 

关于我做错了什么有什么想法吗?

【问题讨论】:

  • 您能否使用dput(twit) 并发布输出以便我们轻松获取它?还有 AFAICS,twitCapture2.txt 包含一些奇怪的东西,所以你可能也想放一个指向 Dropbox 的链接……另外……twit1 是什么? O_o
  • @ttmaccer:是的,在我刚刚抓取 2 行之前,因为我认为实际读取文件不会有问题,但似乎读取文件是问题的一部分。

标签: json r twitter


【解决方案1】:

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

不起作用: {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)

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • Alexanre Nierenbe:我倾向于同意。有什么建议么?只需使用 strigr 包中的 str_replace 函数吗?
    • json是怎么生成的?我会从那里开始。如果是 PHP,我会为您提供解决方案 :)
    • 我在这里使用了最后一个答案:stackoverflow.com/questions/8243681/… 来下载 JSON。我不确定是用什么来生成它的。
    【解决方案3】:

    你试过了吗

    twit1<-fromJSON(twit[1])
    

    【讨论】:

      【解决方案4】:

      我在从 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 对其重新编码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        • 2018-04-25
        • 1970-01-01
        • 2021-09-03
        • 2013-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多