简体   繁体   English

JSON到R data.frame

[英]JSON to R data.frame

I have a problem using rjson package to convert JSON to R data.frame . 我在使用rjson包将JSON转换为R data.frame

I started with: 我开始于:

library("rjson")
json_file <- "btcusd.txt"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

btcusd.txt file contains the following: 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":""}}

Can anyone help me to make it into a data.frame? 谁能帮我把它变成一个data.frame吗?

I'd try jsonlite package. 我会尝试jsonlite包。

Based on what you have I'd try this: 根据您的经验,我会尝试以下方法:

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

mydata <- fromJSON("btcusd.txt")

But, I inspected your data and it's messy. 但是,我检查了您的数据,结果很混乱。 Can you provide some additional information? 您能否提供一些其他信息? if that comes from an API maybe it's easier to read the XML version instead. 如果那是来自API,那么阅读XML版本可能会更容易。

There is an extra comma in your JSON input. JSON输入中有一个逗号。 If you remove the comma it works OK: 如果删除逗号,则可以:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM