将JSON文件发送到服务器以在R中的D3.js中使用它
我有一个这种格式的大数据框架:将JSON文件发送到服务器以在R中的D3.js中使用它,json,r,Json,R,我有一个这种格式的大数据框架: House space type ID less than 18 18 to 23 Greather than 23 1 Livingroom Temperature 1 0 29.44004742 70.55995258 1 Hallway temperature 1 14.59211237 61.59805511 23.80983252 1 Bedroom
House space type ID less than 18 18 to 23 Greather than 23
1 Livingroom Temperature 1 0 29.44004742 70.55995258
1 Hallway temperature 1 14.59211237 61.59805511 23.80983252
1 Bedroom temperature 1 1.683093749 60.63394348 37.68296277
2 Livingroom Temperature 2 17.16494111 49.53457447 33.30048442
2 Hallway temperature 2 36.3833926 49.56992189 14.04668551
2 Bedroom temperature 2 39.74861892 53.78744108 6.463939993
我使用以下代码将其转换为json对象:
library(RJSONIO)
houses<- by(data, list(data$House), function(x) {
outer_template <- '{"id":"House_%s","condition":[%s]}'
inner_template <- '{"type":"%s","segment":{"Less than 18":%s, "18-25":%s , "Greater than 25":%s},"space":"%s"}'
condition <- paste0(apply(x, 1, function(y) {sprintf(inner_template,tolower(y["type"]), y["less.than.18"], y["X18.to.23"],y["Greather.than.23"], tolower(y["space"]))}), collapse=",\n")
sprintf(outer_template, x$House[1], condition)
})
house_json <-(sprintf("[%s]", paste0(houses, collapse=",\n")))
exportJson <- toJSON(house_json, method="C" )
此代码不适用于我,它返回带有“,\”字符的json\u数据输出。比如:
"[{\"id\":\"House_1\",\"condition\":[{\"type\":\"temperature\",\"segment\":{\"Less than 18\": 0.000000, \"18-25\":29.44005 , \"Greater than 25\":70.55995},\"space\":\"livingroom\"},\n{\"type\":\"temperature\",\"segment\":{\"Less than 18\":14.592112, \"18-25\":61.59806 , \"Greater than 25\":23.80983},\"space\":\"hallway\"},\n{\"type\":\"temperature\",\"segment\":{\"Less than 18\": 1.683094, \"18-25\":60.63394 , \"Greater than 25\":37.68296},\"space\":\"bedroom\"}]},\n and so on...
我尝试了近10种不同的方法来删除这些额外的字符(,和反斜杠),但没有任何效果。这对我来说有点失望。非常感谢您的任何帮助。多谢各位
编辑:我需要这样的JSON格式:
{
"id":"House_1",
"condition":
[
{ "type":"temperature",
"segment":{"Less than 18": 0.000000, "18-25":29.44005 , "Greater than 25":70.55995},
"unit":"day",
"space":"livingroom"
},
{ "type":"temperature",
"segment":{"Less than 18":14.592112, "18-25":61.59806 , "Greater than 25":23.80983},
"unit":"day",
"space":"hallway"
},
{ "type":"temperature",
"segment":{"Less than 18": 1.683094, "18-25":60.63394 , "Greater than 25":37.68296},
"unit":"day",
"space":"bedroom"
}
]
},
{
"id":"House_2",
"condition":
[
{ "type":"temperature",
"segment":{"Less than 18":17.16494, "18-25":49.53457 , "Greater than 25":33.30048},
"unit":"day",
"space":"livingroom"
},
{ "type":"temperature",
"segment":{"Less than 18":36.38339, "18-25":49.56992 , "Greater than 25":14.04669},
"unit":"day",
"space":"hallway"
},
{ "type":"temperature",
"segment":{"Less than 18":39.74862, "18-25":53.78744 , "Greater than 25": 6.46394},
"unit":"day",
"space":"bedroom"
}
]
} and so on...
我通常以列表格式生成JSON的R等价物,然后使用toJSON
进行转换。在您的情况下,列表如下所示:
library("RJSONIO")
df <- read.table(textConnection("House space type ID 'less than 18' '18 to 23' 'Greather than 23'
1 Livingroom Temperature 1 0 29.44004742 70.55995258
1 Hallway temperature 1 14.59211237 61.59805511 23.80983252
1 Bedroom temperature 1 1.683093749 60.63394348 37.68296277
2 Livingroom Temperature 2 17.16494111 49.53457447 33.30048442
2 Hallway temperature 2 36.3833926 49.56992189 14.04668551
2 Bedroom temperature 2 39.74861892 53.78744108 6.463939993"),
header=TRUE, stringsAsFactors=FALSE)
output <- list()
for(id in unique(df$ID)){
tmp <- df[df$ID==id, ]
row_list <- list()
for(row in 1:nrow(tmp)){
row_list <- append(row_list,
list(
list("type"=tmp$type[row],
"segment"=list("Less than 18"=tmp$less.than.18[row],
"18-25"=tmp$X18.to.23[row],
"Greater than 25"=tmp$Greather.than.23[row]
),
"unit"="day",
"space"=tmp$space[row]
)
)
)
}
output <- append(output, list(list("id"=paste0("House_", id),
"condition" = row_list)))
}
您只需将数据帧转换为适当的列表格式,然后toJSON
将其转换为有效的JSON…您正在创建一个看起来像JSON的字符串(文本),然后要求将其转换为JSON,这就是您的结果。相反,只需调用JSON(数据)并查看输出。@Cyrille实际上我需要一个自定义格式的JSON文件,作为编辑添加到问题中。对于这种格式,我需要首先定义格式。简单地使用toJSON并不能得到所需的结果。我有一个大数据文件,我们如何将您的逻辑转换为应用于整个数据帧的函数?我用一个循环更新了上面的代码,以创建列表结构。这有点难看,但它完成了任务。。。
library("RJSONIO")
df <- read.table(textConnection("House space type ID 'less than 18' '18 to 23' 'Greather than 23'
1 Livingroom Temperature 1 0 29.44004742 70.55995258
1 Hallway temperature 1 14.59211237 61.59805511 23.80983252
1 Bedroom temperature 1 1.683093749 60.63394348 37.68296277
2 Livingroom Temperature 2 17.16494111 49.53457447 33.30048442
2 Hallway temperature 2 36.3833926 49.56992189 14.04668551
2 Bedroom temperature 2 39.74861892 53.78744108 6.463939993"),
header=TRUE, stringsAsFactors=FALSE)
output <- list()
for(id in unique(df$ID)){
tmp <- df[df$ID==id, ]
row_list <- list()
for(row in 1:nrow(tmp)){
row_list <- append(row_list,
list(
list("type"=tmp$type[row],
"segment"=list("Less than 18"=tmp$less.than.18[row],
"18-25"=tmp$X18.to.23[row],
"Greater than 25"=tmp$Greather.than.23[row]
),
"unit"="day",
"space"=tmp$space[row]
)
)
)
}
output <- append(output, list(list("id"=paste0("House_", id),
"condition" = row_list)))
}
[
{
"id" : "House_1",
"condition" : [
{
"type" : "Temperature",
"segment" : {
"Less than 18" : 0,
"18-25" : 29.44,
"Greater than 25" : 70.56
},
"unit" : "day",
"space" : "Livingroom"
},
{
"type" : "temperature",
"segment" : {
"Less than 18" : 14.592,
"18-25" : 61.598,
"Greater than 25" : 23.81
},
"unit" : "day",
"space" : "Hallway"
},
{
"type" : "temperature",
"segment" : {
"Less than 18" : 1.6831,
"18-25" : 60.634,
"Greater than 25" : 37.683
},
"unit" : "day",
"space" : "Bedroom"
}
]
},
{
"id" : "House_2",
"condition" : [
{
"type" : "Temperature",
"segment" : {
"Less than 18" : 17.165,
"18-25" : 49.535,
"Greater than 25" : 33.3
},
"unit" : "day",
"space" : "Livingroom"
},
{
"type" : "temperature",
"segment" : {
"Less than 18" : 36.383,
"18-25" : 49.57,
"Greater than 25" : 14.047
},
"unit" : "day",
"space" : "Hallway"
},
{
"type" : "temperature",
"segment" : {
"Less than 18" : 39.749,
"18-25" : 53.787,
"Greater than 25" : 6.4639
},
"unit" : "day",
"space" : "Bedroom"
}
]
}
]