为 url 请求发送 json 时的额外斜线 [英] Extra slashes when sending json for url request

查看:26
本文介绍了为 url 请求发送 json 时的额外斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 url 请求发送 json 格式的数据.我的代码如下,以num为输入;

I want to send json format data for a url request. My code is as follows with num as an input;

#* @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.lst 列表如下所示,num=1;

And I use plumber library to make my code as an API. the lst list is like below for num=1;

$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

当我通过 toJSON(lst) 将其转换为 json 时,json 格式是正确的:

When I convert it to json by toJSON(lst) the json format is correct:

{"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]}

{"links":[{"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"}],"directed":[false],"multigraph":[false]}

但是,当我发送 url 请求以获取 json 时,浏览器无法正确诊断 json 格式,并且额外的 slashe_which 我知道这意味着 R 中的空间.url 请求 http://127.0.0.1:8000/getComm?num=1 的响应如下所示;

however, when I send url request to get json, the browser cannot correctly diagnose json format and the are extra slashe_which I know means space in R. the response for the url request http://127.0.0.1:8000/getComm?num=1 is like below;

["{\"links\":[{\"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]}"]

["{\"links\":[{\"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\"}],\"directed\":[false],\"multigraph\":[false]}"]

这些斜线来自哪里?

推荐答案

plumber 将每个端点与一个序列化器"相关联——这个概念今天没有得到很好的记录——默认的序列化器是 JSON.

plumber associates every endpoint with a "serializer" -- a concept that isn't well documented today -- with the default serializer being JSON.

@effel 是正确的,反斜杠正在转义引号.您在响应中看到的是包含 toJSON 的结果的单个字符串.您已经有效地对您的对象进行了双重编码——首先使用您自己的 toJSON 调用来获取一个字符串,然后管道工再次将该字符串编码为 JSON,从而产生反斜杠.

@effel is correct that the backslashes are escaping the quotes. What you're looking at in the response is a single string that contains the result of your toJSON. You've effectively double-encoded your object -- first using your own toJSON call to get a string, then again plumber will encode that string into JSON, resulting in the backslashes.

我怀疑您实际上只想返回对象,而不是 JSON 序列化,您会得到正确的答案.

I suspect you actually just want to return the object, not the JSON serialization and you'll get the right answer.

#* @get /getComm
getComm <- function(num=1){
  #some computation here
  lst<-list(links=linksff,nodes=sc,directed=FALSE,multigraph=FALSE)
  return(lst)
}

如果您确实出于某种原因确实需要执行自己的自定义 JSON 序列化,请参阅有关如何告诉水管工不要序列化您的输出的答案.https://stackoverflow.com/a/44092595/1133019

If you really do need to do your own custom JSON serialization for whatever reason, see this answer on how to tell plumber not to serialize your output. https://stackoverflow.com/a/44092595/1133019

这篇关于为 url 请求发送 json 时的额外斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆