*I'm using yajl_JSON library to produce an NSDictionary from a JSON response for the bit.ly url shortening service.*
The JSON response:
{
errorCode = 0;
errorMessage = "";
results = {
"http://www.example.com/" = {
hash = 4H5keM;
shortKeywordUrl = "";
shortUrl = "http://bit.ly/4BN4qV";
userHash = 4BN4qV;
};
};
statusCode = OK;
}
To clarify, "http://example.com" is not a child "results". And when parsed I have a 3 three nested NSDictionaries.
The problem is that "http://example.com" is arbitrary key. I want to access the keys data without knowing the key. Specifically, so I can get to the value for "shortUrl". How can this this be done effectively? Is there a way to make a keyPath that would be like this:
"results.*.shortUrl"
I have accomplished it by doing the following, but I presume this is not the way it is done:
// Parse the JSON responce
NSDictionary *jsonResponce = [data yajl_JSON];
// Find the missing key
NSString *missingKey = [[[jsonResponce valueForKeyPath:@"results"] allKeys] objectAtIndex:0];
// Log the value for "shortURL"
NSLog(@"It is : %@", [[[jsonResponce objectForKey:@"results"] valueForKeyPath:missingKey] objectForKey:@"shortUrl"]);
If I was using XML, it could be simple to do, which makes me believe I'm not using json/objective-c correctly.
I know it would be possible to store "example.com" in this situation when I make the request to Bit.ly, but... it would be good to know for the future...
Thanks.