我正在尝试使用R中的JSON
包将以下CSV
文件转换为jsonlite
(数据框)。我无法这样做。我正在寻找一种可以解析JSON
任何复杂性和嵌套的通用方法?
library(jsonlite)
fromJSON(json_file)
JSON文件:
{
"IRD": {
"INTV": {
"INTVStatus": "SERV_HST",
"RD": {
"U": "kWh",
"BEV": "0.0379",
"Val": "0",
"RV": "0",
"port": "1"
},
"RD": {
"U": "kWh",
"BEV": "0.0379",
"Val": "0",
"RV": "0",
"port": "2"
},
"RD": {
"U": "Vrms",
"BEV": "231.0000",
"Val": "231.0000",
"RV": "231",
"port": "3"
},
".attrs": {
"GatewayCollectedTime": "2015-12-21T12:05:02.257-05:00",
"INTVSequenceNumber": "47112",
"BlockSequenceNumber": "0",
"EndTime": "2015-12-21T10:00:00.000-05:00"
}
},
"INTV": {
"INTVStatus": "SERV_HST",
"RD": {
"U": "kWh",
"BEV": "0.0379",
"Val": "0",
"RV": "0",
"port": "1"
},
"RD": {
"U": "kWh",
"BEV": "0.0379",
"Val": "0",
"RV": "0",
"port": "2"
},
"RD": {
"U": "Vrms",
"BEV": "231.0000",
"Val": "231.0000",
"RV": "231",
"port": "3"
},
".attrs": {
"GatewayCollectedTime": "2015-12-21T12:05:02.257-05:00",
"INTVSequenceNumber": "47113",
"BlockSequenceNumber": "0",
"EndTime": "2015-12-21T11:00:00.000-05:00"
}
},
"INTV": {
"INTVStatus": "SERV_HST",
"RD": {
"U": "kWh",
"BEV": "0.0379",
"Val": "0",
"RV": "0",
"port": "1"
},
"RD": {
"U": "kWh",
"BEV": "0.0379",
"Val": "0",
"RV": "0",
"port": "2"
},
"RD": {
"U": "Vrms",
"BEV": "231.0000",
"Val": "231.0000",
"RV": "231",
"port": "3"
},
".attrs": {
"GatewayCollectedTime": "2015-12-21T12:05:02.257-05:00",
"INTVSequenceNumber": "47114",
"BlockSequenceNumber": "0",
"EndTime": "2015-12- 21T12:00:00.000-05:00"
}
},
".attrs": {
"NumberINTVs": "3",
"EndTime": "2015-12-21T12:00:00.000-05:00",
"StartTime": "2015- 12-21T09:00:00.000-05:00",
"INTVLength": "60"
}
},
".attrs": {
"Version": "2.0",
"DocumentID": "aebjjjjd-59de-4405-ac0b-50e33b0b4f4b-1",
"JobID": "3354",
"ExportID": "aeb5bf7d-59de-4405-er0b-50e33b0b4f4b",
"RunID": "20430452",
"CreationTime": "2015-12-21T13:55:00.807-05:00",
"StartTime": "2015-12-21T09:55:00.000- 05:00",
"EndTime": "2015-12-21T13:55:00.000-05:00"
}
}
答案 0 :(得分:1)
只需通过多个嵌套结构,考虑逐级解析JSON级别(作业/文档,INTV,RD和attrs),然后将它们绑定到数据帧中:
library(jsonlite)
# READ IN JSON FILE INTO NESTED LIST
ird <- do.call(rbind,
lapply(paste(
readLines("JSONFile.json", warn=FALSE),
collapse=""),
jsonlite::fromJSON))
# JOB
job <- list(ird[[2]])
# INVSTATUS
intvstatus <- lapply(1:3, function(i) ird[[1]][i]$INTV$INTVStatus)
# RDs (nested lapply for three RDs per three INTVs)
rds <- lapply(1:3, function(i)
do.call(rbind, lapply(2:4,
function(j) ird[[1]][i]$INTV[j]$RD)
)
)
# ATTRS
attrs <- lapply(1:3, function(i) ird[[1]][i]$INTV$.attrs)
# BINDING EACH LIST TO FINAL DF (rep() to repeat for each 9 RDs)
df <- data.frame(do.call(rbind, rep(job,9)),
INTVStatus = do.call(rbind, rep(intvstatus, 3)),
do.call(rbind, rds),
do.call(rbind, rep(attrs,3)),
stringsAsFactors=FALSE)
# TO FLATTEN LISTS OUTPUT FROM DO CALLS
df <- data.frame(lapply(df, as.character), stringsAsFactors=FALSE)
# OUTPUT TO CSV
write.csv(df, 'Output.csv')