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

时间:2022-09-15 11:53:51

I'm trying to build a restful API and I'm struggling on how to serialize JSON data to a HTTP query string.

我正在构建一个restful API,我还在为如何将JSON数据序列化为HTTP查询字符串而烦恼。

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

在请求e中需要传递一些强制参数和可选参数。g(下面以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对象编码:

  • jQuery using $.param: -columns[]=name&-columns[]=column&-where[-or][customer_id]=1&-where[-or][services]=schedule&-limit=5&return=column
  • jQuery使用美元。参数:列[]= name&-columns[]= column&-where[或][customer_id]= 1 - -[或][服务]= schedule&-limit = 5返回=列
  • PHP using http_build_query: -columns[0]=name&-columns[1]=column&-where[-or][customer_id]=1&-where[-or][services]=schedule&-limit=5&return=column
  • PHP使用http_build_query:列[0]= name&-columns[1]= column&-where[或][customer_id]= 1 - -[或][服务]= schedule&-limit = 5返回=列
  • Perl using URI::query_form: -columns=name&-columns=column&-where=HASH(0x59d6eb8)&-limit=5&return=column
  • Perl使用URI::query_form:列= name&-columns = column&-where =散列(0 x59d6eb8)&-limit = 5返回=列
  • Perl using complex_to_query: -columns:0=name&-columns:1=column&-limit=5&-where.-or.customer_id=1&-where.-or.services=schedule&return=column
  • Perl使用complex_to_query:列:0 = name&-columns:1 = column&-limit = 5 &-where.-or.customer_id = 1 &-where.-or.services = schedule&return =列

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非常相似。使用complex_to_query的Perl也非常类似于它们。但是没有一个看起来完全一样。

4 个解决方案

#1


40  

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编码(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 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太长,则会遇到URL长度限制问题。在这种情况下,我将使用POST和body(是的,我知道,当您希望获取某些内容时发送POST不是“纯”的,也不适合REST范式,但是基于jsf的域查询语言也不是)。

#2


4  

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 npm and bower, which is why I have been using it.

它可以在npm和bower中使用,这就是为什么我一直在使用它。

#3


2  

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 将JSON序列化为查询字符串的标准化方法?

我和其他客户试过了

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

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

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

#4


2  

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

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

JSON:    {"_id":"5973782bdb9a930533b05cb2","isActive":true,"balance":"$1,446.35","age":32,"name":"Logan Keller","email":"logankeller@artiq.com","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:'$1,446.35',email:'logankeller@artiq.com',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:'$1,446.35',email:'logankeller@artiq.com',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=$1,446.35&age=32&name=Logan Keller&email=logankeller@artiq.com&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=$1,446.35&age:32&name=Logan%20Keller&email=logankeller@artiq.com&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对象表示法。

#1


40  

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编码(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 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太长,则会遇到URL长度限制问题。在这种情况下,我将使用POST和body(是的,我知道,当您希望获取某些内容时发送POST不是“纯”的,也不适合REST范式,但是基于jsf的域查询语言也不是)。

#2


4  

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 npm and bower, which is why I have been using it.

它可以在npm和bower中使用,这就是为什么我一直在使用它。

#3


2  

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 将JSON序列化为查询字符串的标准化方法?

我和其他客户试过了

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

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

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

#4


2  

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

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

JSON:    {"_id":"5973782bdb9a930533b05cb2","isActive":true,"balance":"$1,446.35","age":32,"name":"Logan Keller","email":"logankeller@artiq.com","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:'$1,446.35',email:'logankeller@artiq.com',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:'$1,446.35',email:'logankeller@artiq.com',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=$1,446.35&age=32&name=Logan Keller&email=logankeller@artiq.com&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=$1,446.35&age:32&name=Logan%20Keller&email=logankeller@artiq.com&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对象表示法。