Iphone 在IOS中解析Twitter JSON提要

Iphone 在IOS中解析Twitter JSON提要,iphone,json,ios,Iphone,Json,Ios,我只是在玩iOS中的JSON解析器,作为一个(简单的)示例,它工作得很好。但我想知道人们如何解析(稍微)更复杂的东西,Twitter趋势JSON,比如: { "trends": { "2011-03-13 11:42:17": [ { "events": null, "query": "Fukushima", "promoted_content": nu

我只是在玩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
}
for (int i = 0; i < [luckyNumbers count]; i++)
    [text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];
NSString *date = [[[luckyNumbers valueForKeyPath:@"trends"] allKeys] description];
NSArray *trends = [luckyNumbers objectForKey:@"trends"];
NSLog(@"%@", [trends valueForKeyPath:date]);
如何返回前3个查询?在这个例子中:福岛,丽贝卡黑和珍珠港

使用示例代码,它如下所示:

{
    "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
}
for (int i = 0; i < [luckyNumbers count]; i++)
    [text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];
NSString *date = [[[luckyNumbers valueForKeyPath:@"trends"] allKeys] description];
NSArray *trends = [luckyNumbers objectForKey:@"trends"];
NSLog(@"%@", [trends valueForKeyPath:date]);
这当然会记录关键“趋势”的内容,我如何不仅返回趋势的第一个(也是唯一一个)关键点,而且再向下挖掘一层以返回“查询”的内容

我也试过这样的方法:

{
    "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
}
for (int i = 0; i < [luckyNumbers count]; i++)
    [text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];
NSString *date = [[[luckyNumbers valueForKeyPath:@"trends"] allKeys] description];
NSArray *trends = [luckyNumbers objectForKey:@"trends"];
NSLog(@"%@", [trends valueForKeyPath:date]);

但是不行…

JSON映射到字典和数组,这就是它的重点(相对于XML)。如何从特定JSON文件中获取信息将取决于该文件最初的设计方式

JSON中的方括号表示数组-大括号表示字典(对象)。因此,在您的示例中,我们看到有一个字典包含一个具有键“trends”的对象,一个本身就是字典的对象

如果您记录解析后数组的输出,您将更容易获得这个句柄,因为您将看到NSArray和NSDictionary的整个结构。要做到这一点,解析JSON后,您需要执行以下操作:


NSLog([parsedJsonResult description])

这取决于您使用的JSON解析器

如果使用,将获得dict和数组的
NSDictionary
您需要了解文档结构才能提取所需信息。您可以使用任何常规机制(循环的
、枚举器、谓词等)


如果文档非常大,您可能需要SAX风格的解析器,如。您可以定义方法来在解析每种对象时使用它们。一旦你得到了你想要的东西(3个查询或其他),你就停止解析它速度更快,占用内存更少。但它有点复杂。

控制台提供了非常清晰的解析输出,这很有用,但是我如何从键“趋势”返回字典?好吧,假设解析器返回一个名为
results
的dictionary对象,您可能需要
[results objectForKey:@“trends”]
。实际上,在这个示例中,我试图返回“query”的值,我应该查看objectForKey吗?