用R解析BLS JSON

时间:2014-04-26 00:07:58

标签: json r parsing

我想在R中使用Knitr运行一些预制报告依赖于许多第三方资源,一些作为文本文件提供,一些通过公共API提供。

然而,我并不是特别精通解析JSON文件,并且当它们变得轻微复杂时很快就会失去方向(我不会特别认为我的例子是,无论如何,但仍然。)

这是电话:

library(rjson)
addr = 'http://api.bls.gov/publicAPI/v1/timeseries/data/ENU0607510010'
json_data <- fromJSON(file=addr, method='C')

这就是它的样子 - 任何方式将其填入数据框以进行进一步(自动)融化和绘图?

> str(json_data)[1:100]
List of 4
 $ status      : chr "REQUEST_SUCCEEDED"
 $ responseTime: num 14
 $ message     : list()
 $ Results     :List of 1
  ..$ series:List of 1
  .. ..$ :List of 2
  .. .. ..$ seriesID: chr "ENU0607510010"
  .. .. ..$ data    :List of 35
  .. .. .. ..$ :List of 5
  .. .. .. .. ..$ year      : chr "2013"
  .. .. .. .. ..$ period    : chr "M09"
  .. .. .. .. ..$ periodName: chr "September"
  .. .. .. .. ..$ value     : chr "615958"
  .. .. .. .. ..$ footnotes :List of 1
  .. .. .. .. .. ..$ :List of 2
  .. .. .. .. .. .. ..$ code: chr "P"
  .. .. .. .. .. .. ..$ text: chr "   Preliminary."
  .. .. .. ..$ :List of 5
  .. .. .. .. ..$ year      : chr "2013"
  .. .. .. .. ..$ period    : chr "M08"
  .. .. .. .. ..$ periodName: chr "August"
  .. .. .. .. ..$ value     : chr "615326"
  .. .. .. .. ..$ footnotes :List of 1
  .. .. .. .. .. ..$ :List of 2
  .. .. .. .. .. .. ..$ code: chr "P"
  .. .. .. .. .. .. ..$ text: chr "   Preliminary."
  .. .. .. ..$ :List of 5
  .. .. .. .. ..$ year      : chr "2013"
  .. .. .. .. ..$ period    : chr "M07"
  .. .. .. .. ..$ periodName: chr "July"
  .. .. .. .. ..$ value     : chr "611071"
  .. .. .. .. ..$ footnotes :List of 1
  .. .. .. .. .. ..$ :List of 2
  .. .. .. .. .. .. ..$ code: chr "P"
  .. .. .. .. .. .. ..$ text: chr "   Preliminary."
  .. .. .. ..$ :List of 5

1 个答案:

答案 0 :(得分:1)

放手一搏。我需要在某个时候从RJSONIO转到jsonlite,但这会为您提供数据。这一切都是要弄清楚结构,以便你可以做sapply。我添加了条形图,因为我已经在BLS数据的gist示例中得到了它。

library(RCurl)
library(RJSONIO)
library(ggplot2)

bls.content <- getURLContent("http://api.bls.gov/publicAPI/v1/timeseries/data/ENU0607510010")
bls.json <- fromJSON(bls.content, simplify=TRUE)
tmp <-bls.json$Results[[1]][[1]]
bls.df <- data.frame(year=sapply(tmp$data,"[[","year"),
                     period=sapply(tmp$data,"[[","period"),
                     periodName=sapply(tmp$data,"[[","periodName"),
                     value=as.numeric(sapply(tmp$data,"[[","value")), 
                     stringsAsFactors=FALSE)

head(bls.df, n=10)
##    year period periodName  value
## 1  2013    M09  September 615958
## 2  2013    M08     August 615326
## 3  2013    M07       July 611071
## 4  2013    M06       June 610893
## 5  2013    M05        May 610750
## 6  2013    M04      April 607797
## 7  2013    M03      March 603286
## 8  2013    M02   February 600868
## 9  2013    M01    January 593770
## 10 2012    M13     Annual 586538

gg <- ggplot(data=bls.df, aes(x=year, y=value, group=period)) 
gg <- gg + geom_bar(stat="identity", position="dodge", aes(fill=period))
gg

enter image description here