1

我在使用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?

4

2 回答 2

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))
于 2017-11-21T22:55:38.483 回答
0

我会尝试jsonlite包。

根据你所拥有的,我会试试这个:

install.packages("jsonlite")
library(jsonlite)

mydata <- fromJSON("btcusd.txt")

但是,我检查了你的数据,结果很乱。你能提供一些额外的信息吗?如果它来自 API,那么阅读 XML 版本可能会更容易。

于 2017-11-21T22:35:28.110 回答