R数据帧到数组json

时间:2015-08-21 12:47:40

标签: json r dataframe

我有数据框

id|name | surname
------------------
1 |James| Smith
2 |Mat  | Stone
3 |Stan | Daimon

我需要将其转换为数组json对象(只是字符串)

[
{id:1,name:"James",surname:"Smith"},
{id:2,name:"Mat",surname:"Stone"},
{id:3,name:"Stan",surname:"Daimon"}
]

1 个答案:

答案 0 :(得分:2)

我们可以使用toJSON

中的library(jsonlite)
library(jsonlite)
toJSON(df1)
#[{"id":1,"name":"James","surname":"Smith"},{"id":2,"name":"Mat","surname":"Stone"},{"id":3,"name":"Stan","surname":"Daimon"}] 

数据

df1 <- structure(list(id = 1:3, name = c("James", "Mat", "Stan"),
surname = c("Smith", 
"Stone", "Daimon")), .Names = c("id", "name", "surname"), 
class = "data.frame", row.names = c(NA, -3L))