TouchJSON 序列化字典和数组的结构
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我仔细写下这个问题后,我意识到我错在哪里。我想无论如何我都会发布这个。也许这对其他人来说是一个有用的例子。所以答案是……
我把字典放反了!
DictionaryWithObjectsAndKeys 方法期望值和键相反,因此构建此结构的正确方法是:
当您查看方法名称“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:
This makes sense when you look at the method name "dictionaryWithObjectsAndKeys", but why it isn't done as "dictionaryWithKeysAndObjects" I have no idea.