我有数据框
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"}
]
答案 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))