Domanda

Ok I get and store a Json feed to an array called jsonArray. I then loop over the jsonArray pulling out the array keys and storing them as strings. I then add those strings to an inner dictionary called innerDict, I then add that dictionary to the info dictionary with the key thePostCode using the below. So basically innerDict is stored inside infoDict.

-(void)pointInfo{

infoDict = [[NSMutableDictionary alloc]init];

for (int i = 0; i < jsonArray.count; i++) {

innerDict = [[NSMutableDictionary alloc]init];

info = [[jsonArray objectAtIndex:i]objectForKey:@"inf"];
thePostCode = [[jsonArray objectAtIndex:i]objectForKey:@"pc"];
mail = [[jsonArray objectAtIndex:i]objectForKey:@"mail"];
url = [[jsonArray objectAtIndex:i]objectForKey:@"url"];
type = [[jsonArray objectAtIndex:i]objectForKey:@"items"];

[innerDict setObject:type forKey:@"Items"];
[innerDict setObject:info forKey:@"Info"];
[innerDict setObject:mail forKey:@"Mail"];
[innerDict setObject:url forKey:@"Url"];

[infoDict setObject:innerDict forKey:thePostCode];

}

the output of infoDict looks like this:

infoDict is {
"ME14 4NN" =     {
    Info = "";
    Items = 4;
    Mail = "";
    Url = "";
};
"ME15 6LG" =     {
    Info = af;
    Items = "0,6,9";
    Mail = "";
    Url = "";
};
"ME15 6YE" =     {
    Info = "";
    Items = "4,5,6,7,11";
    Mail = "";
    Url = "";
};
}

Now what I want to do is get the values of the innerDict object i.e "ME15 6YE" above and use that as the query to pull out the associated data for Info, Items, Mail and Url keys. I have been staring at my screen for a few hours but I am just not getting it.

I can pull out the last object of the inner dict using the below however I would like to grab all the values associated with a particular postcode which would be the innerDict key. Completely brain fried at the moment!

for (NSMutableDictionary *dictionary in infoDict) {

    NSLog(@"URL %@", [innerDict objectForKey:@"Url"]);
    NSLog(@"MAIL %@", [innerDict objectForKey:@"Mail"]);
    NSLog(@"ITEMS %@", [innerDict objectForKey:@"Items"]);
    NSLog(@"ITEMS %@", [innerDict objectForKey:@"Info"]);

}
È stato utile?

Soluzione

To get the correct inner dictionary, use objectForKey:

NSDictionary *innerDict = [infoDict objectForKey:@"ME15 6YE"];
NSLog(@"Url %@", [innerDict objectForKey:@"Url"]);
NSLog(@"Mail %@", [innerDict objectForKey:@"Mail"]);
NSLog(@"Items %@", [innerDict objectForKey:@"Items"]);
NSLog(@"Info %@", [innerDict objectForKey:@"Info"]);

Altri suggerimenti

Maybe I'm misunderstanding something, but I think this should answer your questions.

You're using dictionary in the for loop, but trying to access it via innerDict. Change it to:

for (NSMutableDictionary *dictionary in infoDict) {
    NSLog(@"URL %@", [dictionary objectForKey:@"Url"]);
    NSLog(@"MAIL %@", [dictionary objectForKey:@"Mail"]);
    NSLog(@"ITEMS %@", [dictionary objectForKey:@"Items"]);
    NSLog(@"ITEMS %@", [dictionary objectForKey:@"Info"]);
}

Or, for a single one,

NSMutableDictionary *inner = [infoDict objectForKey:@"POST CODE HERE"];
NSLog(@"URL %@", [inner objectForKey:@"Url"]);
NSLog(@"MAIL %@", [inner objectForKey:@"Mail"]);
NSLog(@"ITEMS %@", [inner objectForKey:@"Items"]);
NSLog(@"ITEMS %@", [inner objectForKey:@"Info"]);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top