使用 fromJSON 创建 JSON 时有关意外符号的错误消息
Error message about unexpected symbol while creating JSON with fromJSON
这可能是一件容易的事,但我在创建一个带有简单文本输入的简单 JSON 文件时遇到了很多困难。形成错误消息很明显我正在做某事。引号有误,但我不知道是什么。
library(jsonlite)
# Create an example
jsoncars <- toJSON(mtcars[1:2,], pretty=TRUE)
jsoncars
这给出:
[
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.62,
"qsec": 16.46,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4"
},
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.875,
"qsec": 17.02,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4 Wag"
}
]
现在,我将这段完全相同的文本复制到 fromJSON:
fromJSON("[
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.62,
"qsec": 16.46,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4"
},
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.875,
"qsec": 17.02,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4 Wag"
}
]")
我收到以下错误:
Error: unexpected symbol in:
" {
"mpg"
Execution halted
顺便说一句,fromJSON(jsoncars)
有效,但仍然没有解决我的问题。
有什么想法吗?
使用'
与JSON
文本中的"
区分开来:
jsonlite::fromJSON('[
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.62,
"qsec": 16.46,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4"
},
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.875,
"qsec": 17.02,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4 Wag"
}
]')
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
另一种选择是新的 R>=4.0 raw character string 语法 r"(...)"
:
jsonlite::fromJSON(r"([
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.62,
"qsec": 16.46,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4"
},
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.875,
"qsec": 17.02,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4 Wag"
}
])")
这可能是一件容易的事,但我在创建一个带有简单文本输入的简单 JSON 文件时遇到了很多困难。形成错误消息很明显我正在做某事。引号有误,但我不知道是什么。
library(jsonlite)
# Create an example
jsoncars <- toJSON(mtcars[1:2,], pretty=TRUE)
jsoncars
这给出:
[
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.62,
"qsec": 16.46,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4"
},
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.875,
"qsec": 17.02,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4 Wag"
}
]
现在,我将这段完全相同的文本复制到 fromJSON:
fromJSON("[
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.62,
"qsec": 16.46,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4"
},
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.875,
"qsec": 17.02,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4 Wag"
}
]")
我收到以下错误:
Error: unexpected symbol in:
" {
"mpg"
Execution halted
顺便说一句,fromJSON(jsoncars)
有效,但仍然没有解决我的问题。
有什么想法吗?
使用'
与JSON
文本中的"
区分开来:
jsonlite::fromJSON('[
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.62,
"qsec": 16.46,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4"
},
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.875,
"qsec": 17.02,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4 Wag"
}
]')
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
另一种选择是新的 R>=4.0 raw character string 语法 r"(...)"
:
jsonlite::fromJSON(r"([
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.62,
"qsec": 16.46,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4"
},
{
"mpg": 21,
"cyl": 6,
"disp": 160,
"hp": 110,
"drat": 3.9,
"wt": 2.875,
"qsec": 17.02,
"vs": 0,
"am": 1,
"gear": 4,
"carb": 4,
"_row": "Mazda RX4 Wag"
}
])")