JSON - 角色问题

时间:2017-02-28 13:05:41

标签: json r rmongodb

我使用RMongo库从mongo数据库中提取了一些数据。我一直在处理数据没有问题。但是,我需要访问最初在数据库中保存为JSON的字段。由于rmongodb将数据保存为数据帧,因此我现在有一个长度为1的大字符向量:

res1 = "[ { \"text\" : \"@Kayture Beyoncé jam session ?\" , \"name\" : \"beponcé \xed\xa0\xbc\xed\xbc\xbb\" , \"screenName\" : \"ColaaaaTweedy\" , \"follower\" : false , \"mentions\" : [ \"Kayture\"] , \"userTwitterId\" : \"108061963\"} , { \"text\" : \"@Kayture fucking marry me\" , \"name\" : \"George McQueen\" , \"screenName\" : \"GeorgeMcQueen12\" , \"follower\" : false , \"mentions\" : [ \"Kayture\"] , \"userTwitterId\" : \"67896750\"}]"

我需要从这个数组中提取对象的所有“text”属性(在这个例子中有2个),但我找不到快速的方法。我正在尝试使用strsplit,或使用jsonlite从字符转到json文件,然后列出,但它不起作用。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

开始
res1 = "[ { \"text\" : \"@Kayture Beyoncé jam session ?\" , \"name\" : \"beponcé \xed\xa0\xbc\xed\xbc\xbb\" , \"screenName\" : \"ColaaaaTweedy\" , \"follower\" : false , \"mentions\" : [ \"Kayture\"] , \"userTwitterId\" : \"108061963\"} , { \"text\" : \"@Kayture fucking marry me\" , \"name\" : \"George McQueen\" , \"screenName\" : \"GeorgeMcQueen12\" , \"follower\" : false , \"mentions\" : [ \"Kayture\"] , \"userTwitterId\" : \"67896750\"}]"

您可以使用fromJSON()包中的jsonlite来解析该JSON对象。

library(jsonlite)

fromJSON(res1)

                            text           name      screenName follower mentions userTwitterId
1 @Kayture Beyoncé jam session ? beponcé í ¼í¼»   ColaaaaTweedy    FALSE  Kayture     108061963
2 @Kayture fucking marry me      George McQueen GeorgeMcQueen12    FALSE  Kayture      67896750