views:

554

answers:

2

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?

+3  A: 

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.

Barry Wark
This only provides a 2d dictionary: http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMNSDictionary%2BURLArguments.m I will update the question with more clarification
coneybeare
It's a pretty good start. Perhaps you could look at the code and subclass it, or otherwise replicate it with recursive calls?
Alex Reynolds
What about sending a plist directly (assuming you control the client)? Or using a POST with a plist payload?
Barry Wark
I thought about this too — I was looking for something in Javascript that could create XML or PList for me, but I came up empty. I would need JS to intercept the submit button press, covert the html POST form elements to XML, and post that string to the server.
coneybeare
A: 

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.

Todd Ditchendorf