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
outer_template
inner_template
condition
sprintf(outer_template, x$House[1], condition)
})
house_json
exportJson
我创建了JSON对象,然后以test.json的形式将文件保存到我的系统中。现在我想把它作为JSON文件发送到数据库服务器,以便在某些d3.js代码中直接使用。我写了这以下代码:
writeLines(exportJson, "test.json")
file
json_data
json_data
这个代码不工作对我来说,这将返回“ \”字符json_data输出。像:
"[{\"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...
2016-02-29
USAL
+2
您正在创建一个字符串(文本),看起来像JSON,然后要求将其转换为JSON - 因此您的结果。相反,只需调用JSON(数据)并查看输出。 –
+0
@Cyrille其实我需要JSON文件的自定义格式,我在问题中添加了一个编辑。对于这种格式,我需要首先定义格式。只是使用toJSON并不能提供所需的结果。 –