我有一个带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) ------^
答案 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\"}"
#]