我有以下形式的JSON:
{"abc":
{
"123":[45600],
"378":[78689],
"343":[23456]
}
}
我需要将以上格式的JSON转换为R中的CSV文件。
CSV格式:
ds y
123 45600
378 78689
343 23456
我正在使用R库rjson
这样做。我正在做这样的事情:
jsonFile <- fromJSON(file=fileName)
json_data_frame <- as.data.frame(jsonFile)
但它没有按我需要的方式行事。
答案 0 :(得分:2)
您可以使用(?:....)
将数据读入列表,但您需要将其拆分以将其组合到data.frame中:
jsonlite::fromJSON
答案 1 :(得分:1)
我相信你有json文件阅读器 - 来自JSON函数。
df <- data.frame( do.call(rbind, rjson::fromJSON( '{"a":true, "b":false, "c":null}' )) )
答案 2 :(得分:0)
以下代码从https://takeout.google.com获取Google的位置记录(json)档案。如果您启用了时间线&#39;您的手机上的Google地图中的(位置跟踪)。归功于http://rpubs.com/jsmanij/131030原始代码。请注意,像这样的json文件可能非常大,而plyr :: llply在解析列表时比lapply更有效。 Data.table为我提供了更高效的rbindlist&#39;将列表带到data.table。 Google每天为我记录350到800个GPS通话!多年的地理位置历史记录会从“JSON&#39;”转换为相当大的列表:
format(object.size(doc1),units="MB")
[1] "962.5 Mb"
我找到了&#39; do.call(rbind ..)&#39;未优化。时间戳,纬度和长度需要一些对Google地球专业版有用的工作,但我会被带走。最后,我使用了`write.csv&#39;将data.table转换为CSV。这就是原本所需的OP。
ts lat long latitude longitude
1: 1416680531900 487716717 -1224893214 48.77167 -122.4893
2: 1416680591911 487716757 -1224892938 48.77168 -122.4893
3: 1416680668812 487716933 -1224893231 48.77169 -122.4893
4: 1416680728947 487716468 -1224893275 48.77165 -122.4893
5: 1416680791884 487716554 -1224893232 48.77166 -122.4893
library(data.table)
library(rjson)
library(plyr)
doc1 <- fromJSON(file="LocationHistory.json", method="C")
object.size(doc1)
timestamp <- function(x) {as.list(x$timestampMs)}
timestamps <- as.list(plyr::llply(doc1$locations,timestamp))
timestamps <- rbindlist(timestamps)
latitude <- function(x) {as.list(x$latitudeE7)}
latitudes <- as.list(plyr::llply(doc1$locations,latitude))
latitudes <- rbindlist(latitudes)
longitude <- function(x) {as.list(x$longitudeE7)}
longitudes <- as.list(plyr::llply(doc1$locations,longitude))
longitudes <- rbindlist(longitudes)
datageoms <- setnames(cbind(timestamps,latitudes,longitudes),c("ts","lat","long")) [order(ts)]
write.csv(datageoms,"datageoms.csv",row.names=FALSE)