在R编程中从json字符串中删除反斜杠

时间:2017-05-09 16:48:18

标签: r

我需要从我生成的backslash文件中删除所有jsonjson文件包含以下数据

{\"id\":\"650552e7-d0e2-43fc-8eba-50179830fc58\",\"guid\":\"KB0053\",\"kb_category\":\"General\",\"title\":\"Guides VoIP\",\"link\":\"https://instance_name.service-now.com/kb_sysparm_article=KB0016853\",\"doc_id\":\"KB0853\",\"doc_type\":\"customer:nys\",\"content\":\"nTelephony User Guides VoIPn<U+613C><U+3E30>n<U+613C><U+3E30>nCisco 7911 Phone Reference GuideCisco 9900 Series Phone Reference GuideCisco ITS Locations Unity Voicemail Reference GuideCisco DSHES Locations Unity Reference GuideAvaya 1600 Series Phone Reference Phone Reference GuideCisco 6900 Series Phone Reference Series Phone Reference Guide\",\"keywords\":\"Telephony User Guides VoIP, VOIP\",\"abstract\":\" User Guides VoIP\"}

我尝试过以下但没有运气:

gsub("\\\\", "" ,RNR_json)
gsub("\\\\", "", str)

1 个答案:

答案 0 :(得分:0)

不完全确定这是否能回答您的问题,但问题是R使用"来开始/结束字符串,并且字符串中的任何"都需要进行转义。从这个意义上讲,你的字符串并不真正包含任何真正的反斜杠。字符串中的反斜杠是因为您的字符串包含许多"个。如果您使用jsonlite包来解析JSON字符串,那么所有内容都会正确读取,如下所示

library(jsonlite)
x <- '{\"id\":\"650552e7-d0e2-43fc-8eba-50179830fc58\",\"guid\":\"KB0053\",\"kb_category\":\"General\",\"title\":\"Guides VoIP\",\"link\":\"https://instance_name.service-now.com/kb_sysparm_article=KB0016853\",\"doc_id\":\"KB0853\",\"doc_type\":\"customer:nys\",\"content\":\"nTelephony User Guides VoIPn<U+613C><U+3E30>n<U+613C><U+3E30>nCisco 7911 Phone Reference GuideCisco 9900 Series Phone Reference GuideCisco ITS Locations Unity Voicemail Reference GuideCisco DSHES Locations Unity Reference GuideAvaya 1600 Series Phone Reference Phone Reference GuideCisco 6900 Series Phone Reference Series Phone Reference Guide\",\"keywords\":\"Telephony User Guides VoIP, VOIP\",\"abstract\":\" User Guides VoIP\"}'

result <- fromJSON(x)
result

正确解析字符串,如输出中所示:

> result
$id
[1] "650552e7-d0e2-43fc-8eba-50179830fc58"

$guid
[1] "KB0053"

$kb_category
[1] "General"

$title
[1] "Guides VoIP"

$link
[1] "https://instance_name.service-now.com/kb_sysparm_article=KB0016853"

$doc_id
[1] "KB0853"

$doc_type
[1] "customer:nys"

$content
[1] "nTelephony User Guides VoIPn<U+613C><U+3E30>n<U+613C><U+3E30>nCisco 7911 Phone Reference GuideCisco 9900 Series Phone Reference GuideCisco ITS Locations Unity Voicemail Reference GuideCisco DSHES Locations Unity Reference GuideAvaya 1600 Series Phone Reference Phone Reference GuideCisco 6900 Series Phone Reference Series Phone Reference Guide"

$keywords
[1] "Telephony User Guides VoIP, VOIP"

$abstract
[1] " User Guides VoIP"