删除使用toJSON创建的双引号json

时间:2021-08-31 18:55:10

I use the function toJSON like this to create a json:

我使用这样的函数toJSON来创建一个json:

data<-toJSON(list("tablename"= unbox("test.csv"),
                  "header" = header_df,
                  "data" = test1
))

the result is the following:

结果如下:

data

数据

{"tablename":"test.csv","header":["PassengerId","Pclass","Name","Sex","Age","SibSp","Parch","Ticket","Fare","Cabin","Embarked"],"data":[["892","3","Kelly, Mr. James","male","34.5","0","0","330911","7.8292","","Q"]]}

The problem is that it is adding double quotes for the PassengerID and the Age numbers. If I modify manually the JSON to this:

问题是它为乘客id和年龄号添加了双引号。如果我手工修改JSON为:

data<-'{"tablename":"test.csv","header":["PassengerId","Pclass","Name","Sex","Age","SibSp","Parch","Ticket","Fare","Cabin","Embarked"],"data":[[892,"3","Kelly, Mr. James","male",34.5,"0","0","330911","7.8292","","Q"]]}'

then it is working fine. How can I remove the double quotes of some elements in the JSON when creating the JSON?

然后它运行得很好。如何在创建JSON时删除JSON中某些元素的双引号?

You can find the input data here:

你可以在这里找到输入数据:

Hi, you can find it here link I use the following to load it

嗨,你可以在这里找到链接,我用下面的链接来加载它

test <- read.csv("~/Titanic R/test.csv")
header_df<-names(test)
test<-test[1,]
test1<-as.matrix(test)

1 个解决方案

#1


1  

Well, part of the problem is that you are loosing the proper data classes when you do as.matrix; that converts everything to a character value. Then R no longer remembers what was numeric. It's best to avoid this step.

问题的一部分是当你做。matrix时你失去了合适的数据类;将所有内容转换为字符值。然后R就不记得什么是数字了。最好避免这一步。

To get a result like the one you desire, you need an unnamed, unboxed mixed-type list. Here's an example with your data

为了得到你想要的结果,你需要一个未命名的、未装箱的混合类型列表。这里有一个数据示例。

test <- read.csv("~/../Downloads/test.csv", stringsAsFactors=F)

header_df<-names(test)
test<-test[1,]

library(jsonlite)
data<-toJSON(list("tablename"= unbox("test.csv"),
                  "header" = header_df,
                  "data" = Map(unbox, unname(as.list(test)))
))

data
# {"tablename":"test.csv","header":["PassengerId","Pclass","Name","Sex","Age","SibSp","Parch","Ticket","Fare","Cabin","Embarked"],"data":[892,3,"Kelly, Mr. James","male",34.5,0,0,"330911",7.8292,"","Q"]} 

Based on your comment, if you have more than one row of data, this strategy should work (using magtrittr library to make things easier)

根据您的评论,如果您有不止一行数据,那么这个策略应该是有效的(使用magtrittr库使事情更容易)

test <- read.csv("~/../Downloads/test.csv", stringsAsFactors=F)    
header_df<-names(test)

library(magrittr)
tomixedlist <- . %>% unname %>% lapply(unbox)
dx <- split(test, 1:nrow(test)) %>% lapply(tomixedlist) %>% unname

library(jsonlite)
data<-toJSON(list("tablename"= unbox("test.csv"),
                  "header" = header_df,
                  "data" = dx
))

#1


1  

Well, part of the problem is that you are loosing the proper data classes when you do as.matrix; that converts everything to a character value. Then R no longer remembers what was numeric. It's best to avoid this step.

问题的一部分是当你做。matrix时你失去了合适的数据类;将所有内容转换为字符值。然后R就不记得什么是数字了。最好避免这一步。

To get a result like the one you desire, you need an unnamed, unboxed mixed-type list. Here's an example with your data

为了得到你想要的结果,你需要一个未命名的、未装箱的混合类型列表。这里有一个数据示例。

test <- read.csv("~/../Downloads/test.csv", stringsAsFactors=F)

header_df<-names(test)
test<-test[1,]

library(jsonlite)
data<-toJSON(list("tablename"= unbox("test.csv"),
                  "header" = header_df,
                  "data" = Map(unbox, unname(as.list(test)))
))

data
# {"tablename":"test.csv","header":["PassengerId","Pclass","Name","Sex","Age","SibSp","Parch","Ticket","Fare","Cabin","Embarked"],"data":[892,3,"Kelly, Mr. James","male",34.5,0,0,"330911",7.8292,"","Q"]} 

Based on your comment, if you have more than one row of data, this strategy should work (using magtrittr library to make things easier)

根据您的评论,如果您有不止一行数据,那么这个策略应该是有效的(使用magtrittr库使事情更容易)

test <- read.csv("~/../Downloads/test.csv", stringsAsFactors=F)    
header_df<-names(test)

library(magrittr)
tomixedlist <- . %>% unname %>% lapply(unbox)
dx <- split(test, 1:nrow(test)) %>% lapply(tomixedlist) %>% unname

library(jsonlite)
data<-toJSON(list("tablename"= unbox("test.csv"),
                  "header" = header_df,
                  "data" = dx
))