我将数据发送给想要使用http://json2csharp.com/将我的数据字符串转换为对象数组的C#开发人员。我认为jsonlite软件包对此有好处,但是他没有得到他想要的结果,而且当我检查产生的输出时,它并不是我所期望的。我对情况的解释是
产生的输出x <- list(
a=list( a=1,b='x',c=list("Foo","Bar"), d=as.Date("2015-01-01") ),
b=list( a=2,b='y',c=list("Hello","World"), d=as.Date("2014-12-31") ),
c=list( a=3,b='z',c=NULL, d=as.Date("2016-02-29") )
)
toJSON(x, pretty = FALSE )
是
{"a":{"a":[1],"b":["x"],"c":[["Foo"],["Bar"]],"d":["2015-01-01"]},"b":{"a":[2],"b":["y"],"c":[["Hello"],["World"]],"d":["2014-12-31"]},"c":{"a":[3],"b":["z"],"c":{},"d":["2016-02-29"]}}
将所有子元素数据放入列表中,同时将父命名列表视为命名元素的对象。 R没有与JSON设计的那种类型相同,所以这可能是toJSON的预期行为,但我想要更像的东西
[{"a":1,"b":"x","c":["Foo","Bar"],"d":"2015-01-01"},{"a":2,"b":"y","c":["Hello","World"],"d":"2014-12-31"},{"a":3,"b":"z","c":{},"d":"2016-02-29"}]
如何将R中的列表写入jsonlite中的JSON数组?
答案 0 :(得分:2)
事实证明,取消列表和设置auto_unbox是我一直在寻找的。将代码更改为:
x2 <- list(
list( a=1,b='x',c=list("Foo","Bar"), d=as.Date("2015-01-01") ),
list( a=2,b='y',c=list("Hello","World"), d=as.Date("2014-12-31") ),
list( a=3,b='z',c=NULL, d=as.Date("2016-02-29") )
)
toJSON(x2, auto_unbox = TRUE, pretty = FALSE )
OR
x2 <- unname(x)
toJSON(x2, auto_unbox = TRUE, pretty = FALSE )
导致了我正在寻找的对象数组:
[{"a":1,"b":"x","c":["Foo","Bar"],"d":"2015-01-01"},{"a":2,"b":"y","c":["Hello","World"],"d":"2014-12-31"},{"a":3,"b":"z","c":{},"d":"2016-02-29"}]
但是,我无法为他们命名。