将 JSON 序列化为查询字符串的标准化方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15872658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:51:47  来源:igfitidea点击:

Standardized way to serialize JSON to query string?

jsonserializationquery-stringhttp-get

提问by Andreas

I'm trying to build a restful APIand I'm struggling on how to serialize JSONdata to a HTTP query string.

我正在尝试构建一个宁静的环境,API并且正在努力研究如何将JSON数据序列化为HTTP query string.

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):

请求中需要传递许多强制性和可选参数,例如(以下表示为 JSON 对象):

{
   "-columns" : [
      "name",
      "column"
   ],
   "-where" : {
      "-or" : {
         "customer_id" : 1,
         "services" : "schedule"
      }
   },
   "-limit" : 5,
   "return" : "table"
}

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 对象,但我读到我应该避免它(HTTP GET with request body)。

Any thoughts?

有什么想法吗?

Edit for clarification:

编辑以澄清:

Listing how some different languages encodes the given json object above:

列出一些不同的语言如何编码上面给定的 json 对象:

  • jQueryusing $.param: -columns[]=name&-columns[]=column&-where[-or][customer_id]=1&-where[-or][services]=schedule&-limit=5&return=column
  • PHPusing http_build_query: -columns[0]=name&-columns[1]=column&-where[-or][customer_id]=1&-where[-or][services]=schedule&-limit=5&return=column
  • Perlusing URI::query_form: -columns=name&-columns=column&-where=HASH(0x59d6eb8)&-limit=5&return=column
  • Perlusing complex_to_query: -columns:0=name&-columns:1=column&-limit=5&-where.-or.customer_id=1&-where.-or.services=schedule&return=column
  • 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=column
  • Perl使用URI::query_form:-columns=name&-columns=column&-where=HASH(0x59d6eb8)&-limit=5&return=column
  • Perl使用complex_to_query:-columns:0=name&-columns:1=column&-limit=5&-where.-or.customer_id=1&-where.-or.services=schedule&return=column

jQuery and PHP is very similar. Perl using complex_to_query is also pretty similar to them. But none look exactly the same.

jQuery 和 PHP 非常相似。Perl 使用 complex_to_query 也与它们非常相似。但没有一个看起来完全一样。

采纳答案by akonsu

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}:

URL-encode ( https://en.wikipedia.org/wiki/Percent-encoding) 您的 JSON 文本并将其放入单个查询字符串参数中。例如,如果你想通过{"val": 1}

mysite.com/path?json=%7B%22val%22%3A%201%7D

Note that if your JSON gets too long then you will run into a URL length limitation problem. In which case I would use POSTwith a body (yes, I know, sending a POSTwhen 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 变得太长,那么您将遇到 URL 长度限制问题。在这种情况下,我会使用POST一个 body(是的,我知道,POST当你想要获取某些东西时发送一个不是“纯粹的”并且不适合 REST 范式,但你的域特定的基于 JSON 的查询语言也不是) .

回答by niutech

There is no single standard for JSON to query string serialization, so I made a comparison of some JSON serializersand the results are as follows:

JSON查询字符串序列化没有单一的标准,所以我做了一些JSON序列化器对比,结果如下:

JSON:    {"_id":"5973782bdb9a930533b05cb2","isActive":true,"balance":",446.35","age":32,"name":"Logan Keller","email":"[email protected]","phone":"+1 (952) 533-2258","friends":[{"id":0,"name":"Colon Salazar"},{"id":1,"name":"French Mcneil"},{"id":2,"name":"Carol Martin"}],"favoriteFruit":"banana"}
Rison:   (_id:'5973782bdb9a930533b05cb2',age:32,balance:',446.35',email:'[email protected]',favoriteFruit:banana,friends:!((id:0,name:'Colon Salazar'),(id:1,name:'French Mcneil'),(id:2,name:'Carol Martin')),isActive:!t,name:'Logan Keller',phone:'+1 (952) 533-2258')
O-Rison: _id:'5973782bdb9a930533b05cb2',age:32,balance:',446.35',email:'[email protected]',favoriteFruit:banana,friends:!((id:0,name:'Colon Salazar'),(id:1,name:'French Mcneil'),(id:2,name:'Carol Martin')),isActive:!t,name:'Logan Keller',phone:'+1 (952) 533-2258'
JSURL:   ~(_id~'5973782bdb9a930533b05cb2~isActive~true~balance~'!1*2c446.35~age~32~name~'Logan*20Keller~email~'logankeller*40artiq.com~phone~'*2b1*20*28952*29*20533-2258~friends~(~(id~0~name~'Colon*20Salazar)~(id~1~name~'French*20Mcneil)~(id~2~name~'Carol*20Martin))~favoriteFruit~'banana)
QS:      _id=5973782bdb9a930533b05cb2&isActive=true&balance=,446.35&age=32&name=Logan Keller&[email protected]&phone=+1 (952) 533-2258&friends[0][id]=0&friends[0][name]=Colon Salazar&friends[1][id]=1&friends[1][name]=French Mcneil&friends[2][id]=2&friends[2][name]=Carol Martin&favoriteFruit=banana
URLON:   $_id=5973782bdb9a930533b05cb2&isActive:true&balance=,446.35&age:32&name=Logan%20Keller&[email protected]&phone=+1%20(952)%20533-2258&friends@$id:0&name=Colon%20Salazar;&$id:1&name=French%20Mcneil;&$id:2&name=Carol%20Martin;;&favoriteFruit=banana
QS-JSON: isActive=true&balance=%241%2C446.35&age=32&name=Logan+Keller&email=logankeller%40artiq.com&phone=%2B1+(952)+533-2258&friends(0).id=0&friends(0).name=Colon+Salazar&friends(1).id=1&friends(1).name=French+Mcneil&friends(2).id=2&friends(2).name=Carol+Martin&favoriteFruit=banana

The shortest among them is URL Object Notation.

其中最短的是URL Object Notation

回答by wprl

Another option might be node-querystring. It also uses a similar scheme to the ones you've so far listed.

另一种选择可能是node-querystring。它还使用与您迄今为止列出的方案类似的方案。

It's available in both npmand bower, which is why I have been using it.

它在npm和中都可用bower,这就是我一直在使用它的原因。

回答by Sagar Ranglani

How about you try this sending them as follows:

你试试这个发送它们如何:

http://example.com/api/wtf?
[-columns][]=name&
[-columns][]=column&
[-where][-or][customer_id]=1&
[-where][-or][services]=schedule&
[-limit]=5&
[return]=table&

I tried with a REST Client enter image description here

我尝试使用 REST 客户端 在此处输入图片说明

And on the server side (Ruby with Sinatra) I checked the params, it gives me exactly what you want. :-)

在服务器端(Ruby 和 Sinatra)我检查了参数,它给了我你想要的。:-)

enter image description here

在此处输入图片说明