如何在 Cocoa 中遍历 JSON 文件?
嘿,我知道已经有类似的问题了,尽管没有人具体回答我的问题。
基本上我使用一个小的 JSON 文件作为数据库,如下所示:
{
"dataBase" : [
{"version" : "0.1", "creator" : "unknown", "creationDate" : "2011-05-22 21:29:11 +0200"}],
"clients" : [
{"id" : "0", "name" : "customer1"},
{"id" : "1", "name" : "customer2", "projects" : [
{"id" : "0", "name" : "project1", "timestamps" : [
{"id" : "0", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"},
{"id" : "1", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"},
{"id" : "2", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"}
]},
{"id" : "0", "name" : "project2", "timestamps" : [
{"id" : "0", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"},
{"id" : "1", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"},
{"id" : "2", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"}
]}
]}
]}
我正在使用 YAJL 框架解析 JSON,如下所示:
yajl = [json yajl_JSON];
yajl 在标头中声明为 NSDictionary
如果我要求:
NSLog(@"creator Name: %@" ,[[yajl objectForKey:@"dataBase"] valueForKey:@"creator"]);
我得到这个:
2011-05-24 00:31: 36.887 YAJLParser[1800:903] 创建者姓名:( “未知” )
因为我不知道如何处理这些括号,所以我迷失了,但这只是问题的一小部分。
我的问题是如何访问 customer2 项目 2 的所有时间戳? 我尝试了在搜索论坛中找到的所有内容,但找不到(我设置正确)对我有用的东西......
提前致谢!
Hey gus I know there are similar questions already there though none answers my question specifically.
Basically I use a little JSON file as dataBase like so:
{
"dataBase" : [
{"version" : "0.1", "creator" : "unknown", "creationDate" : "2011-05-22 21:29:11 +0200"}],
"clients" : [
{"id" : "0", "name" : "customer1"},
{"id" : "1", "name" : "customer2", "projects" : [
{"id" : "0", "name" : "project1", "timestamps" : [
{"id" : "0", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"},
{"id" : "1", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"},
{"id" : "2", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"}
]},
{"id" : "0", "name" : "project2", "timestamps" : [
{"id" : "0", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"},
{"id" : "1", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"},
{"id" : "2", "start" : "2011-05-21 21:29:11 +0200", "stop" : "2011-05-22 21:29:11 +0200"}
]}
]}
]}
I am parsing JSON with the YAJL Framework like so:
yajl = [json yajl_JSON];
yajl is declared in the header as an NSDictionary
If I ask for:
NSLog(@"creator Name: %@" ,[[yajl objectForKey:@"dataBase"] valueForKey:@"creator"]);
I get this:
2011-05-24 00:31:36.887 YAJLParser[1800:903] creator Name: (
"unknown"
)
Since I don't know how to deal with these brackets then im lost but that's just a small part of the problem.
My question is how would I access, say all timestamps for customer2 project2?
I tried everything that I'd find searching forums but I couldn't find something that (I setup correctly) did work for me…
Thanks in advance!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请在此处查看格式定义。您会发现
[yajl objectForKey:@"dataBase"]
保存单个项目数组,并且在第一个项目中有关键creator
。[[[yajl objectForKey:@"dataBase"] objectAtIndex:0] valueForKey:@"creator"]
应该返回正确的值。类似地,客户端部分的遍历将是,这是访问项目2时间戳2 –
Go through the format definition here. You will find that
[yajl objectForKey:@"dataBase"]
holds a single item array and in that first item there is keycreator
.[[[yajl objectForKey:@"dataBase"] objectAtIndex:0] valueForKey:@"creator"]
should return the right value. Similarly, the traversal for clients part would be,This is to access project2 timestamp2 –