用 R 解析 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2061897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:24:03  来源:igfitidea点击:

Parse JSON with R

jsonr

提问by Btibert3

I am fairly new to R, but the more use it, the more I see how powerful it really is over SAS or SPSS. Just one of the major benefits, as I see them, is the ability to get and analyze data from the web. I imagine this is possible (and maybe even straightforward), but I am looking to parse JSON data that is publicly available on the web. I am not a programmer by any stretch, so any help and instruction you can provide will be greatly appreciated. Even if you point me to a basic working example, I probably can work through it.

我对 R 相当陌生,但使用它越多,我就越能看到它在 SAS 或 SPSS 上的真正强大之处。在我看来,主要好处之一是能够从网络获取和分析数据。我想这是可能的(甚至可能很简单),但我希望解析网络上公开可用的 JSON 数据。我无论如何都不是程序员,因此您可以提供的任何帮助和指导将不胜感激。即使您向我指出一个基本的工作示例,我也可能可以完成它。

采纳答案by rcs

RJSONIOfrom Omegahat is another package which provides facilities for reading and writing data in JSON format.

Omegahat 的RJSONIO是另一个包,它提供了以 JSON 格式读取和写入数据的工具。

rjsondoes not use S4/S3 methods and so is not readily extensible, but still useful. Unfortunately, it does not used vectorized operations and so is too slow for non-trivial data. Similarly, for reading JSON data into R, it is somewhat slow and so does not scale to large data, should this be an issue.

rjson不使用 S4/S3 方法,因此不容易扩展,但仍然有用。不幸的是,它没有使用矢量化操作,因此对于非平凡数据来说太慢了。类似地,将 JSON 数据读入 R 时,它有点慢,因此无法扩展到大数据,如果这是一个问题的话。

Update(new Package 2013-12-03):

更新(新包 2013-12-03):

jsonlite: This package is a fork of the RJSONIOpackage. It builds on the parser from RJSONIObut implements a different mapping between R objects and JSON strings. The C code in this package is mostly from the RJSONIOPackage, the R code has been rewritten from scratch. In addition to drop-in replacements for fromJSONand toJSON, the package has functions to serialize objects. Furthermore, the package contains a lot of unit tests to make sure that all edge cases are encoded and decoded consistently for use with dynamic data in systems and applications.

jsonlite:这个包是包的一个分支RJSONIO。它建立在解析器的基础上,RJSONIO但在 R 对象和 JSON 字符串之间实现了不同的映射。这个包中的C代码大部分来自RJSONIOPackage,R代码已经从头开始重写。除了直接替换fromJSON和 之外toJSON,该包还具有序列化对象的功能。此外,该软件包包含许多单元测试,以确保所有边缘情况都经过一致的编码和解码,以便与系统和应用程序中的动态数据一起使用。

回答by joscani

The jsonlitepackage is easy to use and tries to convert json into data frames.

jsonlite包易于使用,并试图JSON转换成数据帧。

Example:

例子:

library(jsonlite)

# url with some information about project in Andalussia
url <- 'http://www.juntadeandalucia.es/export/drupaljda/ayudas.json'

# read url and convert to data.frame
document <- fromJSON(txt=url)

回答by Leeth

Here is the missing example

这是缺少的示例

library(rjson)
url <- 'http://someurl/data.json'
document <- fromJSON(file=url, method='C')

回答by pauljeba

The function fromJSON() in RJSONIO, rjson and jsonlite don't return a simple 2D data.frame for complex nested json objects.

RJSONIO、rjson 和 jsonlite 中的 fromJSON() 函数不会为复杂的嵌套 json 对象返回简单的 2D data.frame。

To overcome this you can use tidyjson. It takes in a json and always returns a data.frame. It is currently not availble in CRAN, you can get it here: https://github.com/sailthru/tidyjson

为了克服这个问题,您可以使用tidyjson。它接受一个 json 并始终返回一个 data.frame。它目前在 CRAN 中不可用,您可以在此处获取:https: //github.com/sailthru/tidyjson

Update:tidyjson is now available in cran, you can install it directly using install.packages("tidyjson")

更新:tidyjson 现在在 cran 中可用,你可以直接使用install.packages("tidyjson")

回答by Jason

For the record, rjson and RJSONIO do change the file type, but they don't really parse per se. For instance, I receive ugly MongoDB data in JSON format, convert it with rjson or RJSONIO, then use unlist and tons of manual correction to actually parse it into a usable matrix.

作为记录, rjson 和 RJSONIO 确实更改了文件类型,但它们本身并没有真正解析。例如,我以 JSON 格式接收丑陋的 MongoDB 数据,将其转换为 rjson 或 RJSONIO,然后使用 unlist 和大量手动更正将其实际解析为可用矩阵。

回答by Moby M

Try below code using RJSONIO in console

在控制台中使用 RJSONIO 尝试以下代码

library(RJSONIO)
library(RCurl)


json_file = getURL("https://raw.githubusercontent.com/isrini/SI_IS607/master/books.json")

json_file2 = RJSONIO::fromJSON(json_file)

head(json_file2)