2017-02-28 74 views
0

我使用RMongo庫從mongo數據庫提取了一些數據。我一直在處理數據,沒有問題。但是,我需要訪問原始在數據庫中保存爲JSON的字段。由於rmongodb保存的數據作爲數據幀,我現在有長度爲1的大字符向量:JSON - 字符問題

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\"}]" 

我需要從這個數組中提取的對象的所有的「文本」屬性(有在該例子中是2 ),但我無法找出一個快速的方法。我正在嘗試使用strsplit,或使用jsonlite從字符到json文件,然後列出,但它不起作用。

任何想法?

謝謝!

+0

你說'rmongodb'爲'data.frame'所以現在你有一個特徵向量提供數據 - 我不明白這一點......也許你提供一些代碼你的MongoDB通過如何溝通'rmongodb'。 – ottlngr

+0

當然,對此抱歉...我編輯了我原來的問題,因爲我正在使用RMongo。我使用col < - dbGetQuery(mongo,「collection」,「{}」,0,n)提取集合,其中n是我提取的對象的數量。 col是一個數據框。 – Andres

+0

那麼'res1'是什麼呢? – ottlngr

回答

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 
+0

謝謝!我試圖轉換爲JSON,然後使用fromJSON,我的錯誤。再次感謝! – Andres