使用rjson
包将JSON转换为R data.frame
时出现问题。
我开始时:
library("rjson")
json_file <- "btcusd.txt"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
btcusd.txt
文件包含以下内容:
{"Response":"Success","Type":100,"Aggregated":true,"Data":
[{"time":1510650000,"close":6488.28,"high":6618.69,"low":6482.22,"open":6492.35,"volumefrom":9422.44,"volumeto":61626698.63},
{"time":1510671600,"close":6541,"high":6592.05,"low":6487.35,"open":6549.1,"volumefrom":12618.61,"volumeto":82634018.7},],
"TimeTo":1511298000,"TimeFrom":1510574400,"FirstValueInArray":true,"ConversionType":{"type":"direct","conversionSymbol":""}}
有人可以帮我把它变成data.frame吗?
答案 0 :(得分:0)
我试试jsonlite
包。
根据你所拥有的,我试试这个:
install.packages("jsonlite")
library(jsonlite)
mydata <- fromJSON("btcusd.txt")
但是,我检查了你的数据并且它很混乱。你能提供一些额外的信息吗?如果它来自API,则可能更容易阅读XML版本。
答案 1 :(得分:0)
您的JSON输入中有一个额外的逗号。如果删除逗号就可以了:
json_txt <- '{
"Response": "Success",
"Type": 100,
"Aggregated": true,
"Data": [{
"time": 1510650000,
"close": 6488.28,
"high": 6618.69,
"low": 6482.22,
"open": 6492.35,
"volumefrom": 9422.44,
"volumeto": 61626698.63
}, {
"time": 1510671600,
"close": 6541,
"high": 6592.05,
"low": 6487.35,
"open": 6549.1,
"volumefrom": 12618.61,
"volumeto": 82634018.7
}],
"TimeTo": 1511298000,
"TimeFrom": 1510574400,
"FirstValueInArray": true,
"ConversionType": {
"type": "direct",
"conversionSymbol": ""
}
}'
data.frame(fromJSON(json_txt))