将查询字符串解析为结构化 NSDictionary
我有一个查询字符串: a=1&b=2&c[1]=3&c[2]=4
等...
我想要一个 NSDictionary,其中 a =>; 1 ,
b =>; 2、c=> [3,4]
。请注意,c 的值是一个数组。它还应该能够处理类似 c[1][2]=5
的内容,以创建数组的数组 c => [[5]]
。
当然,我可以通过拆分 &
和 =
来自己完成此操作,但是其他情况(例如数组和数组数组)又如何呢?我想要一个来自 POST 请求查询字符串的结构化 NSDictionary,并且不想重写轮子(如果它已经存在)。
是否有任何类/方法(通过 Apple 或第 3 方)可以将查询字符串解析为结构化 NSDictionary?
I have a query string: a=1&b=2&c[1]=3&c[2]=4
etc…
I want a NSDictionary where a => 1
, b => 2, c => [3,4]
. Notice that that the value for c is an array. It should also be able to handle something like c[1][2]=5
to make an array of arrays c => [[5]]
.
Of course I can do it myself by splitting on the &
and =
, but what about other cases such as arrays and arrays of arrays. I want a structured NSDictionary from a POST request queryString and do not want to rewrite the wheel if this already exists.
Are there any class/method, through Apple or 3rd party, that will parse a query string into a structured NSDictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Google Toolbox for Mac 包含
GTMNSDictionaryURLArgumentsAdditions
NSDictionary
上的类别可能会执行您想要的操作。如果您可以控制查询字符串(在客户端),您可以发送一个编码的 plist,该 plist 可以直接解码为
NSDictionary
。The Google Toolbox for Mac contains a
GTMNSDictionaryURLArgumentsAdditions
category onNSDictionary
which may do what you want.If you have control over the query string (on the client side), you could send an encoded plist which can be decoded directly into an
NSDictionary
.在这种情况下可能有点过头了,但 ParseKit 是一个用 Cocoa 编写的开源标记/解析工具包,用于 Cocoa 应用程序:
http://parsekit .com
您可能会感兴趣。
ParseKit 被清晰地分为两个组件:一个 Tokenizer,然后是一个构建在其之上的高级解析工具包。您可以使用其中一个或两个组件来帮助完成此类任务。但同样,ParseKit 对于这个相对简单的解析任务来说可能有点过分了。
It might be overkill in this case, but ParseKit is an open source tokenizing/parsing toolkit written in Cocoa for Cocoa applications:
http://parsekit.com
it may be of interest to you.
ParseKit is cleanly separated into two components: a Tokenizer and then a high-level parsing toolkit built on top of that. You could use either or both of these components to help with this kind of task. But again, ParseKit may be overkill for this relatively simple parsing task.