Return 具有多个键的 NSDictionary 中对象的值

Return value of object in NSDictionary with several keys

我正在从 NSURLRequest 获取一个字符串。然后我反序列化字符串以将其转换为 NSDictionary,如下所示:

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   if(responseString && responseString.length) {
                                       NSLog(@"%@", responseString);
                                       NSError *jsonError;
                                       NSData *objectData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
                                       NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                                                            options:NSJSONReadingMutableContainers
                                                                                              error:&jsonError];
                                       NSLog(@"STRING DESERIALIZADA=%@",json);

我得到 json NSDictionary 的以下结果:

{
    meta =     {
        limit = 20;
        next = "<null>";
        offset = 0;
        previous = "<null>";
        "total_count" = 1;
    };
    objects =     (
                {
            color = Plata;
            "current_latitude" = "-12.19061989";
            "current_longitude" = "-77.004078";
            employee =             {
                active = 1;
                dni = 78965412;
                id = 2;
                lastname = volongo;
                mail = "rv@alosda.pe";
                name = Ronaldo;
                phone = 12355688;
                photo = "http://example_url/media/employee/default.png";
                "resource_uri" = "/rest/employee/2/";
            };
            id = 1;
            "license_plate" = 2752727;
            model = Toyota;
            "property_card" = "AAA-XXX";
            "resource_uri" = "/rest/clientaxiradio/1/";
            state = 0;
            year = 2014;
        }
    );
}

这一次,结果在 "objects" 键下包含 1 个对象,但将来结果响应中会有更多对象。

我需要知道的是在这种情况下(只有 1 个对象)以及当响应包含多个对象时如何检索每个对象键的值。

谢谢。

最快 方法可能正在使用 NSEnumerator 来获取您想要的值。在下面的示例中,您将看到如何 enumerate 通过所有键以及单个键:

// NSEnumerator
id key = nil;
NSEnumerator *enumerator = [[json allKeys] objectEnumerator];
while ((key = [enumerator nextObject]))
{
    id object = json[key];
    NSLog(@"%@",object); // output all objects values in NSDictionary json
}

// Fast Enumeration
for (id key in [json allKeys])
{
    id object = json[key];
    NSLog(@"%@",object); // output all objects values in NSDictionary json 
}

如果您的 NSDictionary 中有多个 object 键,您可以这样做:

NSDictionary *obj = [json objectForKey:@"object"]
for (id key in obj)
{
    id object = json[key];
    NSLog(@"%@",object); 
   // output all objects values in NSDictionary json from "object" key
}

如果 objectNSDictionary 中的 NSArray:

NSArray *obj = [json objectForKey:@"object"];
    for (id key in obj)
    {
        id object = key;
        NSLog(@"%@",object);
    }

如果你想要一把单独的钥匙object:

NSLog(@"%@",[json objectForKey:@"current_latitude"]); // would output -12.19061989

有关详细信息,请参阅 Apple 开发者网站 Fast Enumeration