我试图建立一个宁静的API
,我正在努力如何将JSON
数据序列化为HTTP query string
。标准化的方式来序列化JSON查询字符串?
有许多问题需要在该请求,例如要传递必需和可选参数(表示为下面JSON对象):
{
"-columns" : [
"name",
"column"
],
"-where" : {
"-or" : {
"customer_id" : 1,
"services" : "schedule"
}
},
"-limit" : 5,
"return" : "table"
}
我需要支持各种数量的不同的客户端,从而我正在寻找一种标准化的方式来将此json对象转换为查询字符串。有没有,它看起来如何?
另一种选择是允许用户只传递邮件正文中的json对象,但我读到应该避免它(HTTP GET with request body)。
有什么想法?
编辑澄清:
清单如何一些不同的语言编码给定的JSON对象以上:使用$.param
jQuery
:-columns [] =名& -columns [] =柱& -where [-or] [customer_id] = 1 & -where [-or] [services] = schedule & -limit = 5 & return = columnPHP
使用http_build_query
:-columns [0] =名& -columns [1] =柱& -where [-OR] [CUSTOMER_ID] = 1 & -where [-OR] [服务] =时间表& -limit = 5 &返回=柱Perl
使用URI::query_form
:-columns =名& -columns =柱& -where = HASH(0x59d6eb8)& -limit = 5 &回报=使用complex_to_query
柱
Perl
:-columns:0 =名& - 列:1 =列& -limit = 5 & -where.-or.customer_id = 1 & -where.-or.services =时间表&回报=柱
jQuery和PHP是非常相似的。使用complex_to_query的Perl也非常类似于它们。但没有一个看起来完全一样。
为什么不只是使用POST请求? – akonsu 2013-04-08 06:34:20
检查是否有帮助, http://api.jquery.com/jQuery.param/ – Jubair 2013-04-08 06:37:36
@akonsu:因为我试图获取(GET)一些数据,而不是POST新数据。 – Andreas 2013-04-08 06:38:11