- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
Rails 和其他框架使用 Ruby 中间件将您发送到服务器的参数解析为嵌套的 Hash
对象。
如果您将这些参数发送到服务器:
person[id] = 1
person[name] = Joe Blow
person[email] = joe.blow@test.com
person[address][street_address] = 123 Somewhere St.
person[address][city] = Chicago
person[address][zip] = 12345
person[other_field][] = 1
person[other_field][] = 2
person[other_field][] = 3
它们被解析为:
{
:person => {
:id => "1",
:name => "Joe Blow",
:email => "joe.blow@test.com",
:address => {
:street_address => "123 Somewhere St.",
:city => "Chicago",
:state => "IL",
:zip => "12345"
},
:other_field => [ 1, 2, 3 ]
}
}
我相信 PHP 也支持这一点。谁能告诉我这个约定叫什么,它来自哪里,还有哪些其他语言支持它? (Perl、Python 等)
最佳答案
我想知道是否有这个约定的名称,但我找不到。
就其值(value)而言,在 Ruby 中执行此操作的中间件是 Rack::Utils
。参见 the source on Github .
有关该主题的更多信息in the Ruby on Rails Guides .
这里是 an interesting ticket关于代码从 Rails 移动到 Rack 中间件。
我在 PHP 源代码中做了一些挖掘,似乎所有的魔法都发生在 main/php_variables.c
源文件中。 SAPI
代码调用此处定义的 php_std_post_handler
方法。这最终调用了 php_register_variable_ex
方法,这是 185 行 C 中复杂的字符串解析(我必须承认 C 不是我的强项)。
我在这里能找到的唯一名称是 php_std_post_handler
,PHP 标准 POST 处理程序。
在 Python 中,默认情况下不支持这种格式。参见 this question here on stackoverflow.com关于这个问题。
Perl 中的CGI
库也不支持这种格式。它确实可以轻松访问单个或多个值,但不能像您的示例中那样访问嵌套值。请参阅 feching the value or values of a single named parameter 上的文档和 fetching the parameter list as a hash .
查看 this question 中关于查询参数解析主题的激烈辩论.默认情况下,Java 不会将 POST 参数的这种“嵌套格式”解析为数据结构。
我研究过这个,但没有找到这种参数解析方式的单一名称。在我研究过的语言中,只有 Ruby 和 PHP 原生支持这种格式。
关于ruby - 这个 Ruby 参数约定从何而来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7578269/
我是一名优秀的程序员,十分优秀!