我想json
为 url 请求发送格式数据。我的代码如下num
作为输入;
#* @get /getComm
getComm <- function(num=1){
library(jsonlite)
#some computation here
lst<-list(links=linksff,nodes=sc,directed=FALSE,multigraph=FALSE)
return(toJSON(lst))
}
我使用plumber
库将我的代码作为 API。num=1的lst
列表如下所示;
$links
source target
1 0 3
2 2 5
3 1 4
$nodes
size score id type
1 10 10 7 circle
2 10 10 179 circle
3 10 10 128 circle
4 10 10 191 circle
5 10 10 239 circle
6 10 10 218 circle
$directed
[1] FALSE
$multigraph
[1] FALSE
当我将它转换为json
jsontoJSON(lst)
格式时是正确的:
{"links":[{"source":0,"target":3},{"source":2,"target":5},{"source":1,"target":4}],"节点":[{"size":10,"score":10,"id":7,"type":"circle"},{"size":10,"score":10,"id":179 ,"type":"circle"},{"size":10,"score":10,"id":128,"type":"circle"},{"size":10,"score":10 ,"id":191,"type":"circle"},{"size":10,"score":10,"id":239,"type":"circle"},{"size":10 ,"score":10,"id":218,"type":"circle"}],"directed":[false],"multigraph":[false]}
但是,当我发送 url 请求以获取 json 时,浏览器无法正确诊断json
格式,并且我知道额外的 slashe_ 这意味着 R 中的空间。 url 请求的响应http://127.0.0.1:8000/getComm?num=1
如下所示;
["{\"链接\":[{\"source\":0,\"target\":3},{\"source\":2,\"target\":5},{\"source \":1,\"target\":4}],\"nodes\":[{\"size\":10,\"score\":10,\"id\":7,\"type \":\"circle\"},{\"size\":10,\"score\":10,\"id\":179,\"type\":\"circle\"},{\ "size\":10,\"score\":10,\"id\":128,\"type\":\"circle\"},{\"size\":10,\"score\" :10,\"id\":191,\"type\":\"circle\"},{\"size\":10,\"score\":10,\"id\":239,\ "type\":\"circle\"},{\"size\":10,\"score\":10,\"id\":218,\"type\":\"circle\"}] ,\"定向\":[false],\"multigraph\":[false]}"]
这些斜线是从哪里来的?