序列化JSON以查询字符串的标准化方法? [英] Standardized way to serialize JSON to query string?
问题描述
我正在尝试构建一个宁静的API
,并且正在努力将如何将JSON
数据序列化为HTTP query string
.
I'm trying to build a restful API
and I'm struggling on how to serialize JSON
data to a HTTP query string
.
在请求中需要传递一些强制性和可选参数,例如(以下表示为JSON对象):
There are a number of mandatory and optional arguments that need to be passed in the request, e.g (represented as a JSON object below):
{
"-columns" : [
"name",
"column"
],
"-where" : {
"-or" : {
"customer_id" : 1,
"services" : "schedule"
}
},
"-limit" : 5,
"return" : "table"
}
我需要支持各种不同的客户端,因此我正在寻找一种标准化的方法来将此json对象转换为查询字符串.有一个,看起来怎么样?
I need to support a various number of different clients so I'm looking for a standardized way to convert this json object to a query string. Is there one, and how does it look?
另一种替代方法是允许用户仅在消息正文中传递json对象,但我读到我应该避免使用它(
Another alternative is to allow users to just pass along the json object in a message body, but I read that I should avoid it (HTTP GET with request body).
有什么想法吗?
编辑以进行澄清:
在上面列出一些不同的语言如何编码给定的json对象:
Listing how some different languages encodes the given json object above:
-
jQuery
使用$.param
:-columns [] = name& -columns [] = column& -where [-or] [customer_id] = 1& -where [-or] [services] = schedule&- limit = 5& return = column -
PHP
使用http_build_query
:-columns [0] = name& -columns [1] = column& -where [-or] [customer_id] = 1& -where [-or] [services] = schedule& ; -limit = 5& return =列 -
Perl
使用URI::query_form
:-columns = name& -columns = column&-其中= HASH(0x59d6eb8)& -limit = 5& return = column -
Perl
使用complex_to_query
:-列:0 =名称&-列:1 =列&-限制= 5& -where.-or.customer_id = 1& -where.-or.services = schedule& return = column
jQuery
using$.param
: -columns[]=name&-columns[]=column&-where[-or][customer_id]=1&-where[-or][services]=schedule&-limit=5&return=columnPHP
usinghttp_build_query
: -columns[0]=name&-columns[1]=column&-where[-or][customer_id]=1&-where[-or][services]=schedule&-limit=5&return=columnPerl
usingURI::query_form
: -columns=name&-columns=column&-where=HASH(0x59d6eb8)&-limit=5&return=columnPerl
usingcomplex_to_query
: -columns:0=name&-columns:1=column&-limit=5&-where.-or.customer_id=1&-where.-or.services=schedule&return=column
jQuery和PHP非常相似.使用complex_to_query的Perl也与它们非常相似.但是没有一个看起来完全一样.
jQuery and PHP is very similar. Perl using complex_to_query is also pretty similar to them. But none look exactly the same.
推荐答案
URL编码( https://en.wikipedia .org/wiki/Percent-encoding ),然后将其放入单个查询字符串参数中.例如,如果您想通过{"val": 1}
:
URL-encode (https://en.wikipedia.org/wiki/Percent-encoding) your JSON text and put it into a single query string parameter. for example, if you want to pass {"val": 1}
:
mysite.com/path?json=%7B%22val%22%3A%201%7D
请注意,如果您的JSON太长,则会遇到URL长度限制问题.在这种情况下,我会在主体上使用POST
(是的,我知道,当您要获取内容时发送POST
并不是纯粹的",也不适合REST范式,但是您的域都不是特定的)基于JSON的查询语言).
Note that if your JSON gets too long then you will run into a URL length limitation problem. In which case I would use POST
with a body (yes, I know, sending a POST
when you want to fetch something is not "pure" and does not fit well into the REST paradigm, but neither is your domain specific JSON-based query language).
这篇关于序列化JSON以查询字符串的标准化方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!