如何从JSON对象(在文件中)读取数据到R?

时间:2021-02-12 15:21:01

I am new to R and do not have much exposure to programming. I am having a problem loading a file(contains JSON object) into R.

我是R的新手,没有太多的编程经验。我正在将一个文件(包含JSON对象)加载到R中。

> library(rjson)
> jsonFile <- "C:\\Users\\jsonRecords.txt"
> jsonData <- fromJSON( jsonFile, method = "C", unexpected.escape = "error" )
Error in fromJSON(jsonFile, method = "C", unexpected.escape = "error") : 
  unexpected character 'C'

I wanted the data to be read into R for further analysis.. Any help will be appreciated.

我希望将数据读入R以作进一步分析。如有任何帮助,我们将不胜感激。

Thanks

谢谢

2 个解决方案

#1


11  

Try just this:

试试这个:

    fromJSON( file = json_file )

It will read all the file. here an example:

它将读取所有文件。下面一个例子:

write(toJSON( iris ),'jstest')
res <- fromJSON( file="jstest")

str(res)
List of 5
 $ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : chr [1:150] "setosa" "setosa" "setosa" "setosa" ...

#2


5  

it looks like all you are missing is the file= argument

看起来你所缺少的就是文件=参数。

fromJSON( file = json_file, method = "C", unexpected.escape = "error" )

if you look at args(fromJSON)

如果查看args(fromJSON)

 > args(fromJSON)
 function (json_str, file, method = "C", unexpected.escape = "error") 

you'll see that the first argument is json_str and the second is file. Since you are only supplying the second argument, you must tell the function explicitly what it is you are giving it. (Otherwise, it thinks that your json_file string is a json object and it will try to treat it as such.. hence the error.)

您将看到第一个参数是json_str,第二个参数是file。既然您只提供第二个参数,那么您必须明确地告诉函数您所提供的是什么。(否则,它认为您的json_file字符串是一个json对象,它将尝试这样对待它。因此错误。)

#1


11  

Try just this:

试试这个:

    fromJSON( file = json_file )

It will read all the file. here an example:

它将读取所有文件。下面一个例子:

write(toJSON( iris ),'jstest')
res <- fromJSON( file="jstest")

str(res)
List of 5
 $ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : chr [1:150] "setosa" "setosa" "setosa" "setosa" ...

#2


5  

it looks like all you are missing is the file= argument

看起来你所缺少的就是文件=参数。

fromJSON( file = json_file, method = "C", unexpected.escape = "error" )

if you look at args(fromJSON)

如果查看args(fromJSON)

 > args(fromJSON)
 function (json_str, file, method = "C", unexpected.escape = "error") 

you'll see that the first argument is json_str and the second is file. Since you are only supplying the second argument, you must tell the function explicitly what it is you are giving it. (Otherwise, it thinks that your json_file string is a json object and it will try to treat it as such.. hence the error.)

您将看到第一个参数是json_str,第二个参数是file。既然您只提供第二个参数,那么您必须明确地告诉函数您所提供的是什么。(否则,它认为您的json_file字符串是一个json对象,它将尝试这样对待它。因此错误。)