如何在R中的数据表中使用单引号处理json

时间:2017-11-26 17:26:19

标签: json r dataframe datatable jsonlite

我有一个带JSON的数据表。我使用jsonlite来处理它。

library(data.table)
library(jsonlite)
a <- data.table(keyword = list("{'id': 2316, 'name': 'women's sexual identity'}"))

要使用fromJSON,我需要将'转换为"

a[, keywords2 := gsub("'", "\"", keyword)]
a[, fromJSON(keywords2, use.names = TRUE, fill = TRUE)]

但由于'中有name,例如'women's sexual identity'。转换后,记录变为{"id": 2316, "name": "women"s sexual identity"}。它会产生错误:

> a[, rbindlist(lapply(keywords2, fromJSON), use.names = TRUE, fill = TRUE)]
Error: lexical error: invalid char in json text.
           {"id": 2316, "name": "women"s sexual identity"}
                     (right here) ------^

1 个答案:

答案 0 :(得分:2)

我们可以试试

a[, keywords2 := gsub("'(?![a-z])|(?<=\\{|\\s)'", '"', keyword, perl = TRUE)]

cat(a$keywords2, '\n')
#{"id": 2316, "name": "women's sexual identity"} 


a[, prettify(toJSON(keywords2))]
#[
# "{\"id\": 2316, \"name\": \"women's sexual identity\"}"
#]