目标 C:如何将嵌套的 NSDictionarys 和 NSArrays 转换为 URLEncoded 字符串 [英] Objective C: how to convert nested NSDictionarys and NSArrays to URLEncoded string

查看:55
本文介绍了目标 C:如何将嵌套的 NSDictionarys 和 NSArrays 转换为 URLEncoded 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Symfony2 项目,为 JQuery Web 客户端和 Objective C iPhone APP 提供服务.

I have a Symfony2 project serving both a JQuery Web client and an Objective C iPhone APP.

JQuery 将其 urlencoded 数据正确发送到服务器.在 Symfony2 控制器中使用 $this->getRequest()->get('myKey'); 正确访问的数据.

JQuery sends properly its urlencoded data to the server. Data that is properly accessed in the Symfony2 Controller using $this->getRequest()->get('myKey');.

因此,数据在application/x-www-form-urlencoded中发送.

为了不改变我的 Symfony 控制器(这恰好是我系统的整个 API,并且有大约 200 个我之前描述的形式的 AJAX 调用),我希望 Objective C 准确地发送数据同样的方式.

In order not to change my Symfony Controllers (that happens to be the the whole API of my system and has around 200 AJAX calls in the form I described before), I'd like the Objective C to send the data exactly in the same way.

我已经部分获得了.我的意思是,我知道如何将简单的 NSDictionary 转换为 URI 参数格式.这就是代码(感谢 Michael Sivolobov).

I have it partially gotten. I mean, I know how to convert a simple NSDictionary to a URI parameters format. This would be the code (Thanks to Michael Sivolobov).

NSMutableArray* parametersArray = [[[NSMutableArray alloc] init] autorelease];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [parametersArray addObject:[NSString stringWithFormat:@"%@=%@", key, obj]];
}];
NSString* parameterString = [parametersArray componentsJoinedByString:@"&"];

但我的问题是知道如何以类似的方式转换一些更复杂的数据.例如嵌套的 NSArrays 和 NSDictionarys(在使用 JSON 时很常见).任何人都知道在 Objective C 中将此类数据转换为 URL(key1=value1&key2=value2 等...)的任何方法??

But my problem know is to know how to convert in a similar way some more complex data. Such as nested NSArrays and NSDictionarys (very usual when working with JSON). Anybody knows any method to convert such data to URL (key1=value1&key2=value2, etc...) in Objective C??

我需要通过 url 发送的数据示例.

example of the data I'd need to send by url.

NSArray *myArray = [NSArray arrayWithObjects: @"Value2", @"Value3", nil];
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys: @"Value1", @"key1", myArray, @"Value2"];

JSON 中的内容是 {"key1": "value1", "key2": ["value2", "value3"]}

Something that in JSON would be {"key1": "value1", "key2": ["value2", "value3"]}

Objective C 中获取该结构并将其转换为 URLencode 的最佳方法是什么,然后再将其传递给 Http POST?我以为会有一些 JSON 或 Cocoa 方法...但什么都没有...

What is the best way in Objective C to get that structure and convert it to URLencode before passing it to the Http POST? I thought there would be some JSON or Cocoa method... but there is nothing...

推荐答案

天啊!为此,我不得不学习一些非常古老的 PHP 知识:http_build_query().

Holy Cow! I had to dust off some really old PHP knowledge for this: http_build_query().

复杂的混合字典/数组对象的格式是base[key][key]…[key]=value

The format of a complex mixed dictionary/array objects is base[key][key]…[key]=value

对于 JSON {"key1": "value1", "key2": ["value2", "value3"]},PHP 期望 key1=value1&key2[0]=value3&key2[1]=value3 作为 URL 编码字符串(所以 key1=value1&key2%5B0%5D=value3&key2%5B1%5D=value3).

For the JSON {"key1": "value1", "key2": ["value2", "value3"]}, PHP would expect key1=value1&key2[0]=value3&key2[1]=value3 as a URL encoded string (so key1=value1&key2%5B0%5D=value3&key2%5B1%5D=value3).

对于更复杂的示例:{"x": {"y": ["a", "b"]}, "z" : "c"},PHP 将期望 <代码>x[y][0]=a&x[y][1]=b&z=c.

For a more complex example: {"x": {"y": ["a", "b"]}, "z" : "c"}, PHP would expect x[y][0]=a&x[y][1]=b&z=c.

这篇关于目标 C:如何将嵌套的 NSDictionarys 和 NSArrays 转换为 URLEncoded 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆