开发者

Parse Query String into a Structured NSDictionary

I have a query string: a=1&b=2&c[1]=3&c[2]=4 etc…

I want a NSDicti开发者_StackOverflow中文版onary 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?


The Google Toolbox for Mac contains a GTMNSDictionaryURLArgumentsAdditions category on NSDictionary 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜