2

我在 R 中的 RJSONIO 包中的 fromJSON 函数有问题。

我有一个用 fromJSON 读取的 json 文件

{"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

谁能建议为什么会这样?

4

2 回答 2

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
于 2018-04-12T14:29:14.193 回答
-1

删除里面有一些不可见的字符indy.json ,问题就解决了。

在此处输入图像描述

于 2018-06-25T03:31:43.760 回答