Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中将datatable转换为一组JSON对象_Json_R - Fatal编程技术网

在R中将datatable转换为一组JSON对象

在R中将datatable转换为一组JSON对象,json,r,Json,R,我想将R中的数据表转换为一组json对象: 假设我的数据表如下所示: name group age (y) height (cm) weight (kg) score [1,] "Doe, John" "Red" "24" "182" "74.8" NA [2,] "Doe, Jane" "Green" "30" "170" "70.1" "500" [3,] "Smith, Joan"

我想将R中的数据表转换为一组json对象: 假设我的数据表如下所示:

name           group    age (y) height (cm) weight (kg) score
[1,] "Doe, John"    "Red"    "24"    "182"       "74.8"      NA   
[2,] "Doe, Jane"    "Green"  "30"    "170"       "70.1"      "500"
[3,] "Smith, Joan"  "Yellow" "41"    "169"       "60"        NA   
[4,] "Brown, Sam"   "Green"  "22"    "183"       "75"        "865"
[5,] "Jones, Larry" "Green"  "31"    "178"       "83.9"      "221"
[6,] "Murray, Seth" "Red"    "35"    "172"       "76.2"      "413"
[7,] "Doe, Jane"    "Yellow" "22"    "164"       "68"        "902"
我希望它如下:

[{"name":"Doe, John","group":"Red","age (y)":24,"height (cm)":182,"weight (kg)":74.8,"score":null},
{"name":"Doe, Jane","group":"Green","age (y)":30,"height (cm)":170,"weight (kg)":70.1,"score":500},
{"name":"Smith, Joan","group":"Yellow","age (y)":41,"height (cm)":169,"weight (kg)":60,"score":null},
{"name":"Brown, Sam","group":"Green","age (y)":22,"height (cm)":183,"weight (kg)":75,"score":865},
{"name":"Jones, Larry","group":"Green","age (y)":31,"height (cm)":178,"weight (kg)":83.9,"score":221},
{"name":"Murray, Seth","group":"Red","age (y)":35,"height (cm)":172,"weight (kg)":76.2,"score":413},
{"name":"Doe, Jane","group":"Yellow","age (y)":22,"height (cm)":164,"weight (kg)":68,"score":902}]

有人能帮我吗?

你确定这是
数据表
输出吗?
[1,]
在我看来很有趣。我认为data.table通常是
1:
。这看起来更像是矩阵输出

但假设它是一个类似data.table的

dd<-data.table(
    name = c("Doe, John", "Doe, Jane", "Smith, Joan", "Brown, Sam",
        "Jones, Larry", "Murray, Seth", "Doe, Jane"), 
    group = c("Red", "Green", "Yellow", "Green", "Green", "Red", "Yellow"), 
    "age (y)" = c(24L, 30L, 41L, 22L, 31L, 35L, 22L),
    "height (cm)" = c(182L, 170L, 169L, 183L, 178L, 172L, 164L), 
    "weight (kg)" = c(74.8, 70.1, 60, 75, 83.9, 76.2, 68), 
    score = c(NA, 500L, NA, 865L, 221L, 413L, 902L)
)

获取所需的输出。

您可以尝试在
jsonline
包上使用
fromJSON
toJSON

library(jsonlite)

iris2 <- fromJSON(myjson)

myjson <- toJSON(iris, pretty=TRUE)
library(jsonlite)

对不起,我的错。你是对的!!我想我发布了一个数据框。你的答案很有效!!非常感谢!!!:)嘿,我看到json对象中的值是用方括号括起来的,比如:“name”:[“Doe,Jane”]有没有可能在没有方括号的情况下得到它们?@rabiyashruff当我运行上面的代码时(使用R版本3.1.0和
rjson_0.2.14
data.table_1.9.2
)。我得到的输出看起来像你想要的。你能举个可复制的例子吗?嗨,我以前确实试过。但它不能提供所需的输出。它只生成一个json对象,所有值用逗号分隔,没有字段名。
library(jsonlite)

iris2 <- fromJSON(myjson)

myjson <- toJSON(iris, pretty=TRUE)