问题描述:

I'm stuck with this JSON which I have to load into Core Data:

{

"versions": {

"group12": "2662ad279e1c4303b534a0f363be7b90",

"group51": "2205e48de5f93c784733ffcca841d2b5",

"place123": "856babf68edfd13e2fd019df330e11c5"

}

}

I don't know how should I cope with this in forin loop. For now I have some code which is only showing me the name of the version e.g. group12, group51 but I can't get those hashes from this JSON...

for (id activity in [responseObject objectForKey:@"versions"]) {

NSLog(@"%@", activity);

}

Probably this will be some very basic stuff but my mind today is a little bit outside.

网友答案:

You get back a NSDictionary * from [responseObject objectForKey:@"versions"]. If you use fast enumeration over an dictionary, you'll only get the keys. But if you want the key and the value use

[[responseObject objectForKey:@"versions"]
    enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop){
  // do something with key & object
}]

If you know, you'll always have NSString * objects in your dictionary, you can specialize the parameters' types. Although this won't change anything during code generation, it'll give you a better code completion and warnings/errors, if you try to use methods on the key or the object NSString doesn't implement.

网友答案:

You are currently logging only the key from the dict,

get the object for that key too

for (id activity in responseObject[@"versions"]) {
    NSLog(@"%@", activity); // key
    NSLog(@"%@", responseObject[@"versions"][activity]); // value

}
相关阅读:
Top