访问由 JSON 框架 iPhone 生成的嵌套数组和字典

发布于 2024-08-17 22:39:32 字数 673 浏览 12 评论 0原文

我正在使用 Google API 返回一些 JSON,我已使用 JSON 框架(Stig B - Google 代码)将其转换为 Objective C 类型。

我现在有这样的结构:

responseData
    results
        [0]
            title = "Stack Overflow"
    cursor

如何访问嵌套数组 results 来获取 title 值(我猜是字典)?

我已经尝试过,但没有成功:

    for (NSString *key in [jsonObjects objectForKey:@"responseData"]) {
        NSLog(@"%@",key);
        for (NSString *element in [key valueForKey:@"results"]) {
            NSLog(@"%@",element);   
        }
    }

外部循环将打印出数组 resultscursor 的名称,这样就可以工作,但对于内部循环,我得到一个 <代码>不符合键值编码错误。

谢谢

I'm using a Google API to return some JSON, which i have converted to their Objective C types using the JSON-framework (Stig B - Google Code).

I now have structures like this:

responseData
    results
        [0]
            title = "Stack Overflow"
    cursor

How can i access the nested array results to get at the title value (dictionary i'm guessing)?

I have tried this but no success:

    for (NSString *key in [jsonObjects objectForKey:@"responseData"]) {
        NSLog(@"%@",key);
        for (NSString *element in [key valueForKey:@"results"]) {
            NSLog(@"%@",element);   
        }
    }

The outer loop will print out the names of the arrays results and cursor so that works, but for the inner loop, I get a not key value coding compliant error.

Thanks

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

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

发布评论

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

评论(1

烛影斜 2024-08-24 22:39:32

您可以使用 NSLog([jsonObjects description]) 来查看字典的内容和结构。
要浏览到“结果”数组及其内容,您可以使用以下(或类似)代码:

NSDictionary* responseDict = [jsonObjects objectForKey:@"responseData"]; // Get your dictionary
NSArray* resultsArray = [responseDict objectForKey:@"results"]; 
for (NSDictionary* internalDict in resultsArray)
    for (NSString *key in [internalDict allKeys])
       NSLog([NSString stringWithFormat:@"%@ - %@", key, [internalDict objectForKey:key];

You can use NSLog([jsonObjects description]) to see the contents and structure of your dictionary.
To browse to your "results" array and its contents you can use the following (or similar) code:

NSDictionary* responseDict = [jsonObjects objectForKey:@"responseData"]; // Get your dictionary
NSArray* resultsArray = [responseDict objectForKey:@"results"]; 
for (NSDictionary* internalDict in resultsArray)
    for (NSString *key in [internalDict allKeys])
       NSLog([NSString stringWithFormat:@"%@ - %@", key, [internalDict objectForKey:key];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文