我正在使用TouchJSON从http://enbr.co.cc/TrailsApp/shops.php检索JSON响应。在我的应用程序中,我使用此代码来处理网址方案。
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url) {
return NO;
}
NSString *urlString = [url absoluteString];
NSString *urlStringDecoded = [urlString stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSArray *list = [urlStringDecoded componentsSeparatedByString:@"="];
NSString *urlPrefix = [list objectAtIndex:0];
NSString *name = [list objectAtIndex:1];
if ([urlPrefix isEqualToString:@"tridetrails://opentrail?name"]) {
TrailViewController *trailViewController = [[TrailViewController alloc] initWithNibName:@"TrailViewController" bundle:[NSBundle mainBundle]];
trailViewController.trailToGoto = name;
[self.navigationController pushViewController:trailViewController animated:YES];
[trailViewController release];
}
if ([urlPrefix isEqualToString:@"tridetrails://openshop?name"]) {
ShopViewController *shopViewController = [[ShopViewController alloc] initWithNibName:@"ShopViewController" bundle:[NSBundle mainBundle]];
shopViewController.shopToGoto = name;
[self.navigationController pushViewController:shopViewController animated:YES];
[shopViewController release];
}
return YES;
}
如何根据NSString名称将从JSON创建的NSDictionary中的正确条目推送到ShopViewController?这是我的字典由NSLog打印出来的NSLog(@“%@”,myObj);.提前谢谢。
{
shops = (
{
blurb = "Bootdoctors blurb";
image = bootdoctorslogo;
locations = "Mountain Village";
motto = "Bootdoctors shop motto";
name = Bootdoctors;
},
{
blurb = "Easy Rider blurb";
image = easyriderlogo;
locations = Telluride;
motto = "Easy Rider shop motto";
name = "Easy Rider";
},
{
blurb = "Paragon Ski & Sport blurb";
image = paragonskiandsportlogo;
locations = Telluride;
motto = "Paragon shop motto";
name = "Paragon Ski & Sport";
},
{
blurb = "Telluride Sports blurb";
image = telluridesportslogo;
locations = "Telluride and Mountain Village";
motto = "Telluride Sports shop motto";
name = "Telluride Sports";
}
);
}
答案 0 :(得分:0)
您可能需要提供有关您尝试执行的操作的更多信息。例如,您没有说明如何检索包含所有商店详细信息的字典以及ShopViewController如何访问此字典。但是,从字典中选择一个名称的商店可以通过以下方式完成:
NSDictionary *jsonResponse; // You don't say how the ShopViewController has access to
// the response so let's just assume a local variable here.
NSDictionary *foundShop = nil; // This will be selected shop after the search below
NSArray *shops = [jsonResponse objectForKey:@'shops'];
for (NSDictionary *shop in shops) {
if ([shop objectForKey:@'name'] isEqualToString:self.shopToGoto]) {
foundShop = shop;
break;
}
}
if (foundShop) {
// Do something with the dictionary keys and values in foundShop
}
else {
// Error condition - shop with required name is not present
// Handle error
}
答案 1 :(得分:0)
您可以使用NSPredicate选择您要查找的商店:
NSString* shopName = ...;
NSArray* shops = ...; // this is your JSON-produced array of NSDictionary Shop objects
NSPredicate* predicate = [NSPredicate predicateWithFormat: @"name == '%@'", shopName ];
NSArray* matchingShops = [shops filteredArrayUsingPredicate: predicate];
NSDictionary* firstMatchingShop = [matchingShops objectAtIndex: 0];