如何在 Cocoa 中遍历 JSON 文件?

发布于 2024-11-09 11:08:57 字数 1527 浏览 4 评论 0原文

嘿,我知道已经有类似的问题了,尽管没有人具体回答我的问题。

基本上我使用一个小的 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 技术交流群。

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

发布评论

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

评论(1

柠檬心 2024-11-16 11:08:57

请在此处查看格式定义。您会发现 [yajl objectForKey:@"dataBase"] 保存单个项目数组,并且在第一个项目中有关键 creator

[[[yajl objectForKey:@"dataBase"] objectAtIndex:0] valueForKey:@"creator"] 应该返回正确的值。类似地,客户端部分的遍历将是,

NSArray *clients = [yajl objectForKey:@"clients"];
for (id client in clients) {
    NSArray *projects = (NSArray *)[client objectForKey:@"projects"];
    for (id project in projects) {
        NSArray *timestamps = (NSArray *)[project objectForKey:@"timestamps"];
        for (id timestamp in timestamps) {
            ...
        }
    }
}

这是访问项目2时间戳2 –

id timestamp = [[[[yajl objectForKey:@"clients"] objectAtIndex:1] objectForKey:@"timestamps"] objectAtIndex:1];

NSLog(@"%@ %@",[timestamp valueForKey:@"start"], [timestamp valueForKey:@"stop"]);

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 key creator.

[[[yajl objectForKey:@"dataBase"] objectAtIndex:0] valueForKey:@"creator"] should return the right value. Similarly, the traversal for clients part would be,

NSArray *clients = [yajl objectForKey:@"clients"];
for (id client in clients) {
    NSArray *projects = (NSArray *)[client objectForKey:@"projects"];
    for (id project in projects) {
        NSArray *timestamps = (NSArray *)[project objectForKey:@"timestamps"];
        for (id timestamp in timestamps) {
            ...
        }
    }
}

This is to access project2 timestamp2 –

id timestamp = [[[[yajl objectForKey:@"clients"] objectAtIndex:1] objectForKey:@"timestamps"] objectAtIndex:1];

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