在 iOS 中(再次)解析 JSON

发布于 2024-10-24 11:26:46 字数 2029 浏览 2 评论 0原文

我只是在 iOS 中使用 JSON 解析器,它作为一个(简单)示例运行良好。但我想知道如何实际解析一些(有点)更复杂的东西,即 Twitter 趋势 JSON,如下所示:

{
    "trends": {
        "2011-03-13 11:42:17": [
            {
                "events": null,
                "query": "Fukushima",
                "promoted_content": null,
                "name": "Fukushima"
            },
            {
                "events": null,
                "query": "Rebecca Black",
                "promoted_content": null,
                "name": "Rebecca Black"
            },
            {
                "events": null,
                "query": "Pearl Harbour",
                "promoted_content": null,
                "name": "Pearl Harbour"
            },
            ...
            {
                "events": null,
                "query": "Magdalena Neuner",
                "promoted_content": null,
                "name": "Magdalena Neuner"
            }
        ]
    },
    "as_of": 1300016537
}

如何返回前 3 个查询?在这个例子中:福岛丽贝卡·布莱克珍珠港

使用示例代码,它是这样的:

for (int i = 0; i < [luckyNumbers count]; i++)
    [text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];

不过,这是一个更简单的提要,可以用同样的方式来实现我正在寻找的内容吗?

我正在尝试返回“查询”的值:

所以我正在这样做:

NSLog(@"%@", [[luckyNumbers objectForKey:@"trends"]);

这当然会记录关键“趋势”的内容,我如何不仅返回趋势的第一个(也是唯一一个)关键还要再向下挖掘一层以返回“查询”的内容?

我也尝试过这样的事情:

NSString *date = [[[luckyNumbers valueForKeyPath:@"trends"] allKeys] description];
NSArray *trends = [luckyNumbers objectForKey:@"trends"];
NSLog(@"%@", [trends valueForKeyPath:date]);

但不行...

编辑(aftet hon Cheng 的回答):

因为我想迭代“趋势”的结果,所以我正在做:

NSDictionary *luckyNumbers = [responseString JSONValue];
NSArray *keys = [[luckyNumbers objectForKey:@"trends"] allKeys];

for (int i =0; i < [keys count]; i++) {

NSLog(@"%@", [keys objectAtIndex:i]);


    }

显然有些东西是错误,因为我没有得到任何结果..

I'm just playing with the JSON parser in iOS it's working fine as a ( simple ) example . But I was wonder how one would actually parse something (a bit) more complicated, the a Twitter trends JSON, like this :

{
    "trends": {
        "2011-03-13 11:42:17": [
            {
                "events": null,
                "query": "Fukushima",
                "promoted_content": null,
                "name": "Fukushima"
            },
            {
                "events": null,
                "query": "Rebecca Black",
                "promoted_content": null,
                "name": "Rebecca Black"
            },
            {
                "events": null,
                "query": "Pearl Harbour",
                "promoted_content": null,
                "name": "Pearl Harbour"
            },
            ...
            {
                "events": null,
                "query": "Magdalena Neuner",
                "promoted_content": null,
                "name": "Magdalena Neuner"
            }
        ]
    },
    "as_of": 1300016537
}

How would one just return the first 3 queries ? in this instance : Fukushima, Rebecca Black and Pearl Harbour .

Using the example code, it goes something like this :

for (int i = 0; i < [luckyNumbers count]; i++)
    [text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];

This is for a much simpler feed though, can it be approached the same way for what I'm looking for ?

I'm trying to return the value of "query":

So I`m doing this :

NSLog(@"%@", [[luckyNumbers objectForKey:@"trends"]);

This of course logs the content of the key "trend", how do I go about returning not only the first ( and only ) key of trends but also dig one more level down to return the content of "query" ??

I've also tried something like this :

NSString *date = [[[luckyNumbers valueForKeyPath:@"trends"] allKeys] description];
NSArray *trends = [luckyNumbers objectForKey:@"trends"];
NSLog(@"%@", [trends valueForKeyPath:date]);

but no go ...

EDIT (aftet honcheng's answer):

Because I want to iterate through the results of "trends", I'm doing :

NSDictionary *luckyNumbers = [responseString JSONValue];
NSArray *keys = [[luckyNumbers objectForKey:@"trends"] allKeys];

for (int i =0; i < [keys count]; i++) {

NSLog(@"%@", [keys objectAtIndex:i]);


    }

Obviously something is wrong because I'm not getting any result ..

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

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

发布评论

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

评论(1

往事随风而去 2024-10-31 11:26:46

JSON 解析器通常返回原生 NSObject,例如 NSDictionary 和 NSArray。解析后,您只需将其视为新对象即可。

[luckyNumbers allKeys] 返回一个 NSArray ["trends", "as_of"]
[[luckyNumbers objectForKey:@"trends"] allKeys"] 返回一个 NSArray ["2011-03-13 11:42:17"]

由于 allKeys 返回一个 NSArray,因此您只需使用 for 循环即可获取所有键。

JSON parser usually returns native NSObjects such as NSDictionary and NSArray. After parsing, you just have to treat it like a new object.

[luckyNumbers allKeys] returns an NSArray ["trends", "as_of"]
[[luckyNumbers objectForKey:@"trends"] allKeys"] returns an NSArray ["2011-03-13 11:42:17"]

Since allKeys returns an NSArray, you can just use a for loop to get all your keys.

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