且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用行内引号将JSON导入R

更新时间:2022-11-03 07:39:34

在R中字符串文字可以使用单引号或双引号定义.
例如

In R strings literals can be defined using single or double quotes.
e.g.

s1 <- 'hello'
s2 <- "world"

当然,如果要在使用双引号定义的字符串文字中包含双引号,则需要转义(使用反斜杠)内部引号,否则R代码解析器将无法检测到字符串的结尾正确(单引号也是如此).
例如

Of course, if you want to include double quotes inside a string literal defined using double quotes you need to escape (using backslash) the inner quotes, otherwise the R code parser won't be able to detect the end of the string correctly (the same holds for single quote).
e.g.

s1 <- "Hello, my name is \"John\""

如果您在控制台上打印(使用cat¹)此字符串,或者将此字符串写入文件,您将获得该字符串的实际面",而不是R文字表示形式,即:>

If you print (using cat¹) this string on the console, or you write this string on a file you will get the actual "face" of the string, not the R literal representation, that is :

> cat("Hello, my name is \"John\"")
Hello, my name is "John"

json解析器读取字符串的实际面",因此,在您的情况下,json读取:

The json parser, reads the actual "face" of the string, so, in your case json reads :

[{"id":"484","comment":"They call me "Bruce""}]

不是(R文字表示形式):

not (the R literal representation) :

"[{\"id\":\"484\",\"comment\":\"They call me \"Bruce\"\"}]" 

话说回来,当字符串中包含引号时,json解析器也需要双引号转义.

That being said, also the json parser needs double-quotes escaping when you have quotes inside strings.

因此,您的字符串应采用以下方式进行修改:

Hence, your string should be modified in this way :

[{"id":"484","comment":"They call me \"Bruce\""}]

如果您只是通过添加反斜杠来修改文件,那么您将完全能够读取json.

If you simply modify your file by adding the backslashes you will be perfectly able to read the json.

请注意,该字符串的相应R文字表示形式为:

Note that the corresponding R literal representation of that string would be :

"[{\"id\":\"484\",\"comment\":\"They call me \\\"Bruce\\\"\"}]"

事实上,这可行:

> fromJSON("[{\"id\":\"484\",\"comment\":\"They call me \\\"Bruce\\\"\"}]")
   id              comment
1 484 They call me "Bruce"


¹ 默认的R print函数(当您在一个值上直接按ENTER时也会调用)返回相应的R字符串文字.如果要打印 actual 字符串,则需要使用print(quote=F,stringToPrint)cat函数.


¹ the default R print function (invoked also when you simply press ENTER on a value) returns the corresponding R string literal. If you want to print the actual string, you need to use print(quote=F,stringToPrint), or cat function.

编辑(在@EngrStudent上评论自动将引号转义的可能性):

EDIT (on @EngrStudent comment on the possibility to automatize quotes escaping) :

Json解析器无法自动将引号转义.
我的意思是,尝试将自己放在计算机的鞋子和图像中,您应该将此(未转义的)字符串解析为json:{ "foo1" : " : "foo2" : "foo3" }

Json parser cannot do quotes escaping automatically.
I mean, try to put yourself in the computer's shoes and image you should parse this (unescaped) string as json: { "foo1" : " : "foo2" : "foo3" }

我看到至少三个可能的转义给出了一个有效的json:
{ "foo1" : " : \"foo2\" : \"foo3" }
{ "foo1\" : " : "foo2\" : \"foo3" }
{ "foo1\" : \" : \"foo2" : "foo3" }

I see at least three possible escaping giving a valid json:
{ "foo1" : " : \"foo2\" : \"foo3" }
{ "foo1\" : " : "foo2\" : \"foo3" }
{ "foo1\" : \" : \"foo2" : "foo3" }

从这个小例子中可以看出,转义对于避免歧义确实是必要的.

As you can see from this small example, escaping is really necessary to avoid ambiguities.

也许,如果您要转义的字符串具有非常特殊的结构,在此结构中您可以(没有不确定性)识别需要转义的双引号,则可以创建自己的自动转义过程,但是您需要从头开始,因为没有内置内容.

Maybe, if the string you want to escape has a really particular structure where you can recognize (without uncertainty) the double-quotes needing to be escaped, you can create your own automatic escaping procedure, but you need to start from scratch, because there's nothing built-in.