当从R中的数据帧生成JSON对象时," in“被转换为”in“。我不知道怎么解决这个问题。任何帮助都将不胜感激!
library(jsonlite)
query_json3 <- data.frame("eClass" = c("Fermentation","Sample", "ResultValue","Experiment"),
"collection" = c("fermentations","samples", "resultValues", "experiments"))
filters1 <- data.frame("field" = "attributes.experiment",
"value" = "EXP-EB-22-019")
filters2 <- data.frame("field" = "originID",
"in" = "fermentations.originID")
filters3 <- data.frame("field" = "SubjectID",
"in" = "samples.id")
filters4 <- data.frame("field" = "type",
"value" = "Small-Scale Screening")
query_json3[1, "filters"][[1]] <- list(filters1)
query_json3[2, "filters"][[1]] <- list(filters2)
query_json3[3, "filters"][[1]] <- list(filters3)
query_json3[4, "filters"][[1]] <- list(filters4)
toJSON(query_json3)输出:
[{"eClass":"Fermentation","collection":"fermentations","filters":[{"field":"attributes.experiment","value":"EXP-EB-22-019"}]},{"eClass":"Sample","collection":"samples","filters":[{"field":"originID","in.":"fermentations.originID"}]},{"eClass":"ResultValue","collection":"resultValues","filters":[{"field":"SubjectID","in.":"samples.id"}]},{"eClass":"Experiment","collection":"experiments","filters":[{"field":"type","value":"Small-Scale Screening"}]}] 期望产出:
[{"eClass":"Fermentation","collection":"fermentations","filters":[{"field":"attributes.experiment","value":"EXP-EB-22-019"}]},{"eClass":"Sample","collection":"samples","filters":[{"field":"originID","in":"fermentations.originID"}]},{"eClass":"ResultValue","collection":"resultValues","filters":[{"field":"SubjectID","in":"samples.id"}]},{"eClass":"Experiment","collection":"experiments","filters":[{"field":"type","value":"Small-Scale Screening"}]}] 发布于 2022-07-13 12:10:31
冒烟的枪不是jsonlite。in是R中的保留关键字,为此,data.frame添加了句点。您可以使用check.names=FALSE
data.frame("field" = "SubjectID",
"in" = "samples.id")
# field in.
# 1 SubjectID samples.id
data.frame("field" = "SubjectID",
"in" = "samples.id",
check.names = FALSE)
# field in
# 1 SubjectID samples.id发布于 2022-07-13 12:14:13
这个问题实际上并不是与jsonlite有关。如果您查看您的dataframe filters3,那么您可以看到列名已经更改了,所以jsonlite正在做它应该做的事情。R中有保留词(例如,参见此处:https://learnetutorials.com/r-programming/identifiers-constants-reserved-words)," in“是一个保留词。您能在不破坏其他地方的情况下更改这个字段的名称吗?如果这个名字在这里引起了问题,那么它也可能在其他地方引起问题。
https://stackoverflow.com/questions/72965440
复制相似问题