如何在 JSON 对象中为查询字符串形成数组?
当在查询字符串中发送 JSON 对象中的数组时,每个数组元素是否应该具有相同的键?例如,如果我有这个 JSON 对象:
{"sodas[]": ["coke", "sprite", "fanta"]}
查询字符串是否应该像这样,并且所有键都完全相同 (sodas%5B%5D
)?
sodas%5B%5D=coke&sodas%5B%5D=sprite&sodas%5B%5D=fanta
或者查询字符串中是否应该包含索引值或其他内容(sodas%5B0%5D
、sodas%5B1%5D
等)?
sodas%5B0%5D=coke&sodas%5B1%5D=sprite&sodas%5B2%5D=fanta
When sending an array in a JSON object in a query string, is each array element supposed to have the same key? For example, if I have this JSON object:
{"sodas[]": ["coke", "sprite", "fanta"]}
Should the query string look like this, with all the keys exactly the same (sodas%5B%5D
)?
sodas%5B%5D=coke&sodas%5B%5D=sprite&sodas%5B%5D=fanta
Or should the query strings have an index value in them or something (sodas%5B0%5D
, sodas%5B1%5D
, etc)?
sodas%5B0%5D=coke&sodas%5B1%5D=sprite&sodas%5B2%5D=fanta
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个没有“sodas”键方括号的语句可以工作。我不确定您使用的是哪种语言,但这里有一个 HTML、jQuery 和 PHP 的示例。
HTML(文件:y.html)
JavaScript escape() 函数将 json POST 参数格式化如下(取自 Firebug。)
PHP(文件:y.php)
浏览器输出显示 PHP 的 var_dump() 字符串表示形式对象,单键关联数组,其值为三个汽水品牌的数组。
The first statement without the square braces for the "sodas" key would work. I'm not sure which languages you are using but here is an example with HTML, jQuery, and PHP.
HTML (file: y.html)
The JavaScript escape() function formatted the json POST parameter as follows (taken from Firebug.)
PHP (file: y.php)
The browser output displays the var_dump()'d string representation of a PHP object, single-keyed associative array with the value being an array of three soda brands.