我在R的RJSONIO包中的fromJSON函数有问题。
我有一个json文件可以用fromJSON
读取{"indy movies" :[
{
"name" : "Raiders of the Lost Ark",
"year" : 1981,
"actors" : {
"Indiana Jones": "Harrison Ford",
"Dr. René Belloq": "Paul Freeman"
},
"producers": ["Frank Marshall", "George Lucas", "Howard Kazanjian"],
"budget" : 18000000,
"academy_award_ve": true
},
{
"name" : "Indiana Jones and the Temple of Doom",
"year" : 1984,
"actors" : {
"Indiana Jones": "Harrison Ford",
"Mola Ram": "Amish Puri"
},
"producers": ["Robert Watts"],
"budget" : 28170000,
"academy_award_ve": true
},
{
"name" : "Indiana Jones and the Last Crusade",
"year" : 1989,
"actors" : {
"Indiana Jones": "Harrison Ford",
"Walter Donovan": "Julian Glover"
},
"producers": ["Robert Watts", "George Lucas"],
"budget" : 48000000,
"academy_award_ve": false
}]}
文件名为" indy.json"
这是一个可重复的例子:
indy <- fromJSON(content = "indy.json")
然而,我得到了结果:
> indy <- fromJSON(content = "indy.json")
Error in nchar(content) : invalid multibyte string, element 1
这是我的相关sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.4
任何人都可以建议为什么会这样吗?
答案 0 :(得分:1)
请尝试使用jsonlite软件包。我将您的字符串粘贴到一个文件中,将其保存为t.R
,然后将其读回。字符串表示为您的字符串。
> library(jsonlite)
> library(readr)
> x <- read_file("t.R")
> x
[1] "{\"indy movies\" :[\r\n {\r\n \"name\" : \"Raiders of the Lost Ark\",\r\n \"year\" : 1981,\r\n \"actors\" : {\r\n \"Indiana Jones\": \"Harrison Ford\", \r\n \"Dr. René Belloq\": \"Paul Freeman\" \r\n },\r\n \"producers\": [\"Frank Marshall\", \"George Lucas\", \"Howard Kazanjian\"],\r\n \"budget\" : 18000000,\r\n \"academy_award_ve\": true\r\n },\r\n {\r\n \"name\" : \"Indiana Jones and the Temple of Doom\",\r\n \"year\" : 1984,\r\n \"actors\" : {\r\n \"Indiana Jones\": \"Harrison Ford\", \r\n \"Mola Ram\": \"Amish Puri\"\r\n },\r\n \"producers\": [\"Robert Watts\"],\r\n \"budget\" : 28170000,\r\n \"academy_award_ve\": true\r\n },\r\n {\r\n \"name\" : \"Indiana Jones and the Last Crusade\",\r\n \"year\" : 1989,\r\n \"actors\" : {\r\n \"Indiana Jones\": \"Harrison Ford\", \r\n \"Walter Donovan\": \"Julian Glover\"\r\n },\r\n \"producers\": [\"Robert Watts\", \"George Lucas\"],\r\n \"budget\" : 48000000,\r\n \"academy_award_ve\": false\r\n }]}"
> jsonlite::fromJSON(x)
$`indy movies`
name year actors.Indiana Jones actors.Dr. René Belloq actors.Mola Ram
1 Raiders of the Lost Ark 1981 Harrison Ford Paul Freeman <NA>
2 Indiana Jones and the Temple of Doom 1984 Harrison Ford <NA> Amish Puri
3 Indiana Jones and the Last Crusade 1989 Harrison Ford <NA> <NA>
actors.Walter Donovan producers budget academy_award_ve
1 <NA> Frank Marshall, George Lucas, Howard Kazanjian 18000000 TRUE
2 <NA> Robert Watts 28170000 TRUE
3 Julian Glover Robert Watts, George Lucas 48000000 FALSE
答案 1 :(得分:-1)