Objective C从URL中读取JSON到NSDictionary中

因此,我正在制作一个获取用户当前位置的应用程序,然后将纬度和经度坐标发送到此URL以查看附近的所需商店。 我能够做我刚刚提到的一切,但不知道如何阅读网页内容的内容,并用它来形成一个表格视图/按最短距离排列等我做了一些研究,并计算出这是JSON,并设法parsingURL中的内容到NSJSONSerialization NSDictionary。 我不确定的是如何读取包含JSON语法的NSDictionary的内容。 我需要像“名称”,“距离”,“格式化地址”的键,并按照sorting的“距离”按升序排列我的数据在一个UITableView。

以下是我从URL获取的JSON脚本的屏幕截图: JSON屏幕

这是如何访问不同场地的NSArray

  NSArray* locationsArray = jsonDictionary[@"response"][@"venues"]; 

从那里你可以访问所有的位置数据。 我会用CLLocation属性为这些位置创build一个新的模型类来存储经度和纬度以及距离属性。 遍历数组并实例化模型,同时计算距离设备当前位置的距离:

 NSMutableArray *locationObjectsArray; for (NSDictionary *locationDict in locationsArray) { Location *newLocation = [Location alloc] initWith...]; newLocation.distance = [newLocation.coordinate distanceFromLocation:deviceLocation]; [locationObjectsArray addObject:newLocation]; } NSArray * results = [sortedLocations sortedArrayUsingDescriptors:@[[[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES]]]; 

这应该给你一个按距离sorting的位置数组,你现在可以在表中显示。