jsonlite :: fromJSON()或格式错误的JSON数据的语法?

时间:2015-11-05 08:35:16

标签: json r jsonlite

我正在读取json格式的数据源,无法将其解析为我想要的数据帧。

jsontxt <- '{"sitesEnergy":{"timeUnit":"DAY","unit":"Wh","count":2,"siteEnergyList":[{"siteId":159864,"energyValues":{"measuredBy":"METER","values":[{"date":"2015-09-01 00:00:00","value":2.0},{"date":"2015-09-02 00:00:00","value":2.0}]}},{"siteId":177606,"energyValues":{"measuredBy":"INVERTER","values":[{"date":"2015-09-01 00:00:00","value":null},{"date":"2015-09-02 00:00:00","value":0.0}]}}]}}'
fromJSON(jsontxt,flatten=TRUE)

的产率:

$sitesEnergy
$sitesEnergy$timeUnit
[1] "DAY"

$sitesEnergy$unit
[1] "Wh"

$sitesEnergy$count
[1] 2

$sitesEnergy$siteEnergyList
  siteId energyValues.measuredBy                             energyValues.values
1 159864                   METER  2015-09-01 00:00:00, 2015-09-02 00:00:00, 2, 2
2 177606                INVERTER 2015-09-01 00:00:00, 2015-09-02 00:00:00, NA, 0

前七行输出文本看起来很好,但energyValues.values的值是日期和值的连接版本。我期待这样的事情:

  siteId energyValues.measuredBy    energyValues.values.date   energyValues.values.value
1 159864                   METER  2015-09-01 00:00:00                2
2 159864                   METER  2015-09-02 00:00:00                2
3 177606                INVERTER  2015-09-01 00:00:00                NA
2 177606                INVERTER  2015-09-02 00:00:00                0

myJSON数据包也是格式错误的,我是否正确地使用了fromJSON,我是否需要预处理jsontxt,还是完全是其他的?

我试过了:

fromJSON(jsontxt,simplifyVector = FALSE)

但它返回一个列表而不是我需要的数据帧。我也尝试过不使用flatten = TRUE参数而且不影响输出。

1 个答案:

答案 0 :(得分:1)

不确定这是否是你想要的......

    library(jsonlite)

    jsontxt <- '{"sitesEnergy":{"timeUnit":"DAY","unit":"Wh","count":2,"siteEnergyList":[{"siteId":159864,"energyValues":{"measuredBy":"METER","values":[{"date":"2015-09-01 00:00:00","value":2.0},{"date":"2015-09-02 00:00:00","value":2.0}]}},{"siteId":177606,"energyValues":{"measuredBy":"INVERTER","values":[{"date":"2015-09-01 00:00:00","value":null},{"date":"2015-09-02 00:00:00","value":0.0}]}}]}}'
    jsontxt<-fromJSON(jsontxt,flatten=TRUE)

    str(jsontxt[[1]][4])

    mydf<- jsontxt[[1]][4][[1]]

    library(tidyr)

    unnest(mydf, energyValues.values)