使用多个键在NSDictionary中返回对象的值

时间:2015-03-07 16:25:54

标签: ios json nsdictionary

我从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;
        }
    );
}

这次,结果包含“对象”键下的1个对象,但将来结果响应中会有更多对象。

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

谢谢。

1 个答案:

答案 0 :(得分:0)

最快的方法可能正在使用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 
}

如果object中有多个NSDictionary密钥,则可以执行以下操作:

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
}

object中的NSArrayNSDictionary

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 Developer网站上的Fast Enumeration