我正在使用 yajl_JSON 库从 bit.ly url 缩短服务的 JSON 响应中生成 NSDictionary。
JSON 响应:
{
errorCode = 0;
errorMessage = "";
results = {
"http://www.example.com/" = {
hash = 4H5keM;
shortKeywordUrl = "";
shortUrl = "http://bit.ly/4BN4qV";
userHash = 4BN4qV;
};
};
statusCode = OK;
}
澄清一下,“ http://example.com ”不是子“结果”。解析后,我有 3 个三个嵌套的 NSDictionaries。
问题是“ http://example.com ”是任意键。我想在不知道密钥的情况下访问密钥数据。具体来说,我可以得到“shortUrl”的值。如何有效地做到这一点?有没有办法制作一个像这样的keyPath:
"results.*.shortUrl"
我通过执行以下操作完成了它,但我认为这不是它的完成方式:
// 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"]);
如果我使用 XML,它可能很简单,这让我相信我没有正确使用 json/objective-c。
我知道当我向 Bit.ly 提出请求时,可以在这种情况下存储“example.com”,但是......将来很高兴知道......
谢谢。