将JSON中的未转义的反斜杠读入R中 [英] Reading unescaped backslashes in JSON into R

查看:174
本文介绍了将JSON中的未转义的反斜杠读入R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Facebook Graph API Explorer中读取一些数据到R中进行一些文本分析。但是,JSON Feed中看起来像是未转义的反斜杠,这导致rjson到barf。以下是导致问题的输入类型的最小例子。

I'm trying to read some data from the Facebook Graph API Explorer into R to do some text analysis. However, it looks like there are unescaped backslashes in the JSON feed, which is causing rjson to barf. The following is a minimal example of the kind of input that's causing problems.

library(rjson)
txt <- '{"data":[{"id":2, "value":"I want to \\"post\\" a picture\\video"}]}'
fromJSON(txt)

(请注意, \\ \\video 将在解析后转换为单个反斜杠,这是我实际的数据。)

(Note that the double backslashes at \\" and \\video will convert to single backslashes after parsing, which is what's in my actual data.)

我也尝试了RJSONIO软件包,它也给出了错误,甚至还崩溃了R。

I also tried the RJSONIO package which also gave errors, and even crashed R at times.

有没有人遇到这个问题?有没有办法解决这个短暂的手动追捕每一个错误的错误?有可能的兆字节的JSON被解析,错误消息不是很有用的问题输入的位置。

Has anyone come across this problem before? Is there a way to fix this short of manually hunting down every error that crops up? There's potentially megabytes of JSON being parsed, and the error messages aren't very informative about where exactly the problematic input is.

推荐答案

问题是您正在尝试解析无效的JSON:

The problem is that you are trying to parse invalid JSON:

library(jsonlite)
txt <- '{"data":[{"id":2, "value":"I want to \\"post\\" a picture\\video"}]}'
validate(txt)

问题是 picture\\\video 部分,因为 \v 不是有效的JSON转义序列,即使它是R和其他一些语言中的有效转义序列。也许你的意思是:

The problem is the picture\\video part because \v is not a valid JSON escape sequence, even though it is a valid escape sequence in R and some other languages. Perhaps you mean:

library(jsonlite)
txt <- '{"data":[{"id":2, "value":"I want to \\"post\\" a picture\\/video"}]}'
validate(txt)
fromJSON(txt)

任何一种问题都是在生成无效JSON的JSON数据源中。如果这个数据真的来自Facebook,你发现了一个API的错误。但更有可能您无法正确检索。

Either way to problem is at the JSON data source that is generating invalid JSON. If this data really comes form Facebook, you found a bug in their API. But more likely you are not retrieving it correctly.

这篇关于将JSON中的未转义的反斜杠读入R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆