尝试将推文转换为 json 格式以插入 mongoDB

Trying to convert tweets to json format to insert into mongoDB

> orioles.tweets <- searchTwitter('#orioles', n=15, lang="en")
> orioles.text=laply(orioles.tweets,function(t) t$getText())
> class(toJSON(orioles.text))
[1] "character"

为什么会这样?

不清楚为什么您会认为 class 应该是 "json"。据推测,您正在使用具有 toJSON 函数的 R 包之一。选择 rjsom 包作为测试用例,我发现 ?toJSON-help 页面中的第一个示例将 "character" 作为返回对象的 class:

> ??toJSON
> library(rjson); x <- list( alpha = 1:5, beta = "Bravo", 
+            gamma = list(a=1:3, b=NULL), 
+            delta = c(TRUE, FALSE) )
> json.vec <- toJSON( x )
> class(json.vec)
[1] "character"

如果由于 class 设置不正确而导致后续处理出错,您可以分配一个 class:

class(json.vec) <- "json"

通常不建议这样做,但在 json-wrangling 库的选择与另一包作者选择的不同的情况下可能需要这样做。