Parse Query String into a Structured NSDictionary

荒凉一梦 提交于 2019-12-10 11:33:40

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1626876/parse-query-string-into-a-structured-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!