将 JSON 序列化为查询字符串的标准化方法? [英] Standardized way to serialize JSON to query string?

查看:22
本文介绍了将 JSON 序列化为查询字符串的标准化方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个安静的 API,我正在努力研究如何将 JSON 数据序列化为 HTTP 查询字符串.

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 对象,但我读到我应该避免它(带有请求正文的 HTTP GET).

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

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

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

推荐答案

URL-encode (https://en.wikipedia.org/wiki/Percent-encoding) 您的 JSON 文本并将其放入单个查询字符串参数中.例如,如果你想传递 {"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屋!

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