我是新來操縱R中的json數組當我寫一個json數組使用R包jsonlite到一個.json文件使用下面的代碼,我得到整個json數組打印在該文件的第一行(reg是一個data.frame)。r - jsonlite添加回車結束json數組「父元素」
rownames(reg) <- NULL
write(toJSON(reg), file = "test.json")
我希望能夠添加一個回車「\ n」在每一個重大(「母公司」)在嵌套層次元素的結束,所以不是它看起來像下面:
[{"val":"ID1","prop":{"Sub":{"val":"foo"}},
{"val":"ID2","prop":{"Sub":{"val":"bar"}}]
代替:
[{"val":"ID1","prop":{"Sub":{"val":"foo"}},{"val":"ID2","prop":{"Sub":{"val":"bar"}}]
誰能幫助我?
注:我不想要「漂亮」的佈局。我希望每個父元素/所有子元素屬性都有一行。
下面是一個例子data.frame
reg <- data.frame(value=c("ID1", "ID2", "ID3"), properties.Subject.value=c("http://example.org/ID1", "http://example.org/ID2", "http://example.org/ID3"), properties.Subject.properties.value=c("http://example.org/xID1", "http://example.org/xID2", "http://example.org/xID3"))
value properties.Subject.value properties.Subject.properties.value ID1 http://example.org/ID1 http://example.org/xID1 ID2 http://example.org/ID2 http://example.org/xID2 ID3 http://example.org/ID3 http://example.org/xID3
在'?write'裏面說有一個sep參數。您可以嘗試添加'sep =「\ n」',但我不確定它會起作用。 – lmo
我忽略了你的要求,你不想要漂亮的佈局。因此刪除了答案。你可以添加一個示例數據框嗎? – user5249203
添加數據幀示例! – areyoujokingme