2

我从

   mentions = GET(final_url, sig)
   json = content(mentions)

我的代码在下一行崩溃

   json2 = jsonlite::fromJSON(toJSON(json))

给出错误...

    Error: lexical error: invalid character inside string.
      Foundation and 42nd President of the United States. Follow 
                 (right here) ------^

我正在处理一些 JSON 数据。一小块看起来像这样。也就是说,这是我的变量“json”的输出。

    Lots of JSON before this....

    $statuses[[99]]$retweeted_status$user$location
    [1] "New York, NY"

    $statuses[[99]]$retweeted_status$user$description
    [1] "Founder, Clinton Foundation and 42nd President \003of the United                States. Follow @clintonfdn for \003more on my work around the world."

    $statuses[[99]]$retweeted_status$user$url
    [1] "http://t.co/gI8TIlAJHk"

如您所见,其中一个 JSON 数据中嵌入了转义字符 \003。

我在同一个文件中处理数百条好的信息,但现在任何地方都可能发生这种情况。当然这次它发生在“描述”中,但它可能发生在推文、位置、描述等中。

在尝试执行 jsonlite::fromJSON(toJSON()) 以避免我的代码在这里崩溃之前,有没有办法从 JSON 中“清除”转义字符?

4

1 回答 1

2

你可以尝试这样的事情:

 json2 <- gsub("[\001-\026]*", "", json)

这是一个简单的“策略测试”

> gsub("[\003-\005]*", '', "\003\004\005\027abc")
[1] "\027abc"

如果您需要更好的测试,您应该发布dput(head(json)).

于 2015-10-30T18:11:04.000 回答