使用 TouchJSON 反序列化复杂的 JSON 结果(字典数组)
昨晚我用 TouchJSON 做了一些测试,对于简单的情况来说,它通常运行得很好。 我使用以下代码从文件中读取一些 JSON 内容,并将其反序列化:
NSString *jsonString = [[NSString alloc] initWithContentsOfFile:@"data.json"];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
NSDictionary *items = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
NSLog(@"total items: %d", [items count]);
NSLog(@"error: %@", [error localizedDescription]);
如果文件中有一个非常简单的 JSON 对象(即字典),那么效果很好:
{"id": "54354", "name": "boohoo"}
这样我就能够访问值数组,因为我想根据列表中的索引获取项目:
NSArray *items_list = [items allValues];
NSString *name = [items_list objectAtIndex:1];
(我知道我可以使用字典 API 获取名称)
现在我想反序列化一个半-复杂的 JSON 字符串,表示字典数组。 下面是此类 JSON 字符串的示例:
[{"id": "123456", "name": "touchjson"}, {"id": "3456", "name": "bleh"}]
当我尝试针对 data.json 文件中的新内容运行上面相同的代码时,我没有得到任何结果。 我的 NSLog() 调用显示“总项目数:0”,并且 NSError 对象中没有返回任何错误。
关于发生什么事的任何线索吗? 我完全不知道该怎么做,因为 TouchJSON 没有太多可用的文档,使用示例也更少。
I did a few tests with TouchJSON last night and it worked pretty well in general for simple cases. I'm using the following code to read some JSON content from a file, and deserialize it:
NSString *jsonString = [[NSString alloc] initWithContentsOfFile:@"data.json"];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
NSDictionary *items = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
NSLog(@"total items: %d", [items count]);
NSLog(@"error: %@", [error localizedDescription]);
That works fine if I have a very simple JSON object in the file (i.e. a dictionary):
{"id": "54354", "name": "boohoo"}
This way I was able to get access to the array of values, as I wanted to get the item based on its index within the list:
NSArray *items_list = [items allValues];
NSString *name = [items_list objectAtIndex:1];
(I understand that I could have fetched the name with the dictionary API)
Now I would like to deserialize a semi-complex JSON string, which represents an array of dictionaries. An example of such a JSON string is below:
[{"id": "123456", "name": "touchjson"}, {"id": "3456", "name": "bleh"}]
When I try to run the same code above against this new content in the data.json file, I don't get any results back. My NSLog() call says "total items: 0", and no error is coming back in the NSError object.
Any clues on what is going on? I'm completely lost on what to do, as there isn't much documentation available for TouchJSON, and much less usage examples.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我是 TouchJSON 的作者。
最外层的对象应该是字典而不是数组。 除了字典之外的任何东西都是不合法的。 如果您必须将数组作为最外层对象,则使用该方法(技术上已弃用,但不会很快出现)
请参阅:http://www.json.com/json-schema-proposal/ 了解有关合法 JSON 和非法 JSON 的更多信息。
I'm the author of TouchJSON.
Your outermost object should be a dictionary and NOT an array. Anything other than a dictionary is not legal. If you have to have an array as the outermost object then use the method (which is technically deprecated, but isn't going any where soon)
See: http://www.json.com/json-schema-proposal/ for more information abotu what is and is not legal JSON.
这不是答案,而是指向不同框架的指针:
http://code. google.com/p/json-framework/
我最近经常使用它,对来自第三方服务(例如 Google Local)以及我自己的 Objective-C 和之间的复杂数据结构进行序列化和反序列化Perl 代码绝对没有问题。 更不用说 API 的处理起来非常简单。
祝你好运!
This isn't an answer, but a pointer to a different framework:
http://code.google.com/p/json-framework/
I've been using it quite a bit lately, serializing and de-serializing complex data structures from third-party services such as Google Local and between my own Objective-C and Perl code with absolutely no problems. Not to mention that the API is ridiculously easy to deal with.
Good luck!
@Mathieu - 我认为这就是你正在寻找的东西(晚了 6 个月,我知道:),但我刚刚遇到了同样的问题)
从这里复制并粘贴:http://groups.google.com/group/touchcode-dev/browse_thread/thread/ada885832019f45b
提供更多上下文,我正在解析的 JSON 是一般形式
的:
@Mathieu - I think this is what you are looking for (6 months late, I know :), but I just ran into the same problem)
Copy and pasted from here: http://groups.google.com/group/touchcode-dev/browse_thread/thread/ada885832019f45b
To give more context, the JSON that I'm parsing is in the general form
of:
不确定它是否对您有帮助,但请检查一下
http://tempered.mobi/%20
Not sure if it helps you but check this out
http://tempered.mobi/%20
JSON 的核心是处理对象,反序列化的代码应该如下所示,
它可以与最新的结帐一起使用。
At it's heart JSON deals with objects, your code to de-serialize should be as follows
which does work with the latest checkout.