Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top