我有一串JSON格式的数据,我只是想把它保存为数据帧。这是我的JSON数据:
newobject <-
{
"data": [
{
"links": [
{
"rel": "self",
"href": "https://api.nike.com/v1/me/sport/activities/8102000000014097347280007666912117966998"
}
],
"activityId": "8102000000014097347280007666912117966998",
"activityType": "RUN",
"startTime": "2015-07-25T13:27:52Z",
"activityTimeZone": "America/Anguilla",
"status": "COMPLETE",
"deviceType": "SPORTWATCH",
"metricSummary": {
"calories": "1012",
"fuel": "3338",
"distance": "13.059499740600586",
"steps": "0",
"duration": "1:05:27.000"
},
"tags": [],
"metrics": []
}
],
"paging": {
"next": "/v1/me/sport/activities/RUNNING?count=1&access_token=Qr7kEcwD6bpfxy2mJ1yH8uGBGfdY&offset=2",
"previous": null
}
}
)
当我尝试将其保存到R中的对象时,出现错误:
答案 0 :(得分:2)
首先,您必须将对象定义为R中的字符串,只需使用简单的引号...
newobject <-
'{
"data": [
{
"links": [
{
"rel": "self",
"href": "https://api.nike.com/v1/me/sport/activities/8102000000014097347280007666912117966998"
}
],
"activityId": "8102000000014097347280007666912117966998",
"activityType": "RUN",
"startTime": "2015-07-25T13:27:52Z",
"activityTimeZone": "America/Anguilla",
"status": "COMPLETE",
"deviceType": "SPORTWATCH",
"metricSummary": {
"calories": "1012",
"fuel": "3338",
"distance": "13.059499740600586",
"steps": "0",
"duration": "1:05:27.000"
},
"tags": [],
"metrics": []
}
],
"paging": {
"next": "/v1/me/sport/activities/RUNNING?count=1&access_token=Qr7kEcwD6bpfxy2mJ1yH8uGBGfdY&offset=2",
"previous": null
}
}'
之后,您可以使用jsonlite
包(install.packages("jsonlite")
)将字符串转换为数据框。
library(jsonlite)
df <- fromJSON(newobject)
然后,如果您运行df$data
,则会获得data.frame。