TouchJSON 序列化字典和数组的结构

发布于 2024-12-21 19:13:45 字数 1402 浏览 1 评论 0原文

iPhone 开发问题(ObjectiveC)。

我正在尝试使用 TouchJSON 库,但在序列化为 JSON 时遇到一些问题。我已打开 ARC,因此我使用 来自 github 的 ARC 分支。 我我正在尝试我想象的一个相当基本的嵌套结构。里面有三个字典,一个字典里面有一个数组。

//Make some dictionaries with simple string pairs
NSDictionary *dicA = [NSDictionary dictionaryWithObjectsAndKeys:@"x", @"1", @"y", @"2", nil];
NSDictionary *dicB = [NSDictionary dictionaryWithObjectsAndKeys:@"x", @"1", @"y", @"2", nil];
NSDictionary *dicC = [NSDictionary dictionaryWithObjectsAndKeys:@"x", @"1", @"y", @"2", nil];

//Make an array of dictionary objects
NSArray *saveArray = [NSArray arrayWithObjects:dicA, dicB, dicC, nil];

//Make dictionary which has that array as one of the values
NSDictionary *bigDic = [NSDictionary dictionaryWithObjectsAndKeys:@"arr", saveArray,
                                                                  @"mmm", @"nnn", nil];

NSData *jsonData = [[CJSONSerializer serializer] serializeObject:saveArray error:NULL];
//This works '[{"1":"x","2":"y"},{"1":"x","2":"y"},{"1":"x","2":"y"}]'

NSData *jsonDataB = [[CJSONSerializer serializer] serializeObject:bigDic error:NULL];
//This fails

当我尝试序列化 bigDic 时,它在运行时崩溃,并显示以下内容:

'NSInvalidArgumentException',原因:'-[__NSArrayI UTF8String]: 无法识别的选择器发送到实例

在上面的行上序列化数组似乎工作正常。我的 bigDic 出了什么问题?

iPhone development question (ObjectiveC).

I'm trying to use TouchJSON library, and having some trouble serialising to JSON. I have ARC switched on so I'm using the ARC branch from github. I'm trying what I imagine to be a fairly a basic nested structure. Three dictionaries inside and array inside a dictionary.

//Make some dictionaries with simple string pairs
NSDictionary *dicA = [NSDictionary dictionaryWithObjectsAndKeys:@"x", @"1", @"y", @"2", nil];
NSDictionary *dicB = [NSDictionary dictionaryWithObjectsAndKeys:@"x", @"1", @"y", @"2", nil];
NSDictionary *dicC = [NSDictionary dictionaryWithObjectsAndKeys:@"x", @"1", @"y", @"2", nil];

//Make an array of dictionary objects
NSArray *saveArray = [NSArray arrayWithObjects:dicA, dicB, dicC, nil];

//Make dictionary which has that array as one of the values
NSDictionary *bigDic = [NSDictionary dictionaryWithObjectsAndKeys:@"arr", saveArray,
                                                                  @"mmm", @"nnn", nil];

NSData *jsonData = [[CJSONSerializer serializer] serializeObject:saveArray error:NULL];
//This works '[{"1":"x","2":"y"},{"1":"x","2":"y"},{"1":"x","2":"y"}]'

NSData *jsonDataB = [[CJSONSerializer serializer] serializeObject:bigDic error:NULL];
//This fails

When I try to serialize bigDic it bombs out at runtime with the following:

'NSInvalidArgumentException', reason: '-[__NSArrayI UTF8String]:
unrecognized selector sent to instance

Serializing an array on the line above seems to work OK. What's wrong with my bigDic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2024-12-28 19:13:45

当我仔细写下这个问题后,我意识到我错在哪里。我想无论如何我都会发布这个。也许这对其他人来说是一个有用的例子。所以答案是……

我把字典放反了!

DictionaryWithObjectsAndKeys 方法期望值和键相反,因此构建此结构的正确方法是:

//Make some dictionaries with simple string pairs
NSDictionary *dicA = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"x", @"2", @"y", nil];
NSDictionary *dicB = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"x", @"2", @"y", nil];
NSDictionary *dicC = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"x", @"2", @"y", nil];

//Make an array of dictionary objects
NSArray *saveArray = [NSArray arrayWithObjects:dicA, dicB, dicC, nil];

//Make dictionary which has that array as one of the values
NSDictionary *bigDic = [NSDictionary dictionaryWithObjectsAndKeys:saveArray, @"arr",
                                                                  @"nnn", @"mmm", nil];

当您查看方法名称“dictionaryWithObjectsAndKeys”时,这是有意义的,但为什么它不作为“dictionaryWithKeysAndObjects”完成我没有主意。

After carefully writing out this question I realised where I had gone wrong. Thought I'd post this anyway. Maybe it's a helpful example for others. So the answer is...

I have my dictionaries back to front!

The dictionaryWithObjectsAndKeys method expects the values and keys the other way around so the correct way to build this structure is:

//Make some dictionaries with simple string pairs
NSDictionary *dicA = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"x", @"2", @"y", nil];
NSDictionary *dicB = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"x", @"2", @"y", nil];
NSDictionary *dicC = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"x", @"2", @"y", nil];

//Make an array of dictionary objects
NSArray *saveArray = [NSArray arrayWithObjects:dicA, dicB, dicC, nil];

//Make dictionary which has that array as one of the values
NSDictionary *bigDic = [NSDictionary dictionaryWithObjectsAndKeys:saveArray, @"arr",
                                                                  @"nnn", @"mmm", nil];

This makes sense when you look at the method name "dictionaryWithObjectsAndKeys", but why it isn't done as "dictionaryWithKeysAndObjects" I have no idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文