-3

So I am making an application that gets the users current location, then sends the Latitude and Longitude coordinates to this URL to see which desired stores are nearby. I am able to do everything I just mentioned, but am not sure how to read the content of the content of the webpage and use it to form a table view/ arrange it by shortest distance etc. I did some research and figured out this is JSON, and managed to parse the content from the URL into a NSDictionary with NSJSONSerialization. What I am not sure about is how to read the content of the NSDictionary containing the JSON syntax. I need keys like 'name', 'distance', 'formatted address', and to arrange my data in a UITableView by sorted 'distance' in ascending order.

Here is a screenshot of the JSON script i GET from the URL:JSON SCREENSHOT

Tal Zamirly
  • 117
  • 1
  • 11
  • 1
    What have you tried so far? What's the question? You access the keys of the parsed JSON like any other dictionary, so I'm not sure what you want. – Jon Shier Feb 11 '15 at 01:44
  • 2
    There are literally thousands of prior questions here about how to access values out of a JSON payload. (Which is quite strange, really, given that doing this is blazingly simple and should be intuitive.) – Hot Licks Feb 11 '15 at 01:49
  • I am not sure how to read the dictionary. I want to have an array of venues, which contains their name, address, etc. @jshier – Tal Zamirly Feb 11 '15 at 01:50
  • possible duplicate of [How do I parse JSON with Objective-C?](http://stackoverflow.com/questions/5547311/how-do-i-parse-json-with-objective-c) – Hot Licks Feb 11 '15 at 01:50
  • 1
    And go to json.org and learn the JSON syntax. It only takes 5-10 minutes to learn. – Hot Licks Feb 11 '15 at 01:51
  • My general process for dealing with a JSON API is to use AFNetworking for the networking and JSON parsing and Mantle to create my model objects, which are created from the JSON responses in the AFNetworking completion blocks. – Jon Shier Feb 11 '15 at 01:57
  • do you want to populate your UITableView with the objects in the "venues" array ? – Vivek Seth Feb 11 '15 at 02:10
  • @VivekSeth Yes, and Organise them by distance, displaying the name, address and distance – Tal Zamirly Feb 11 '15 at 03:40

1 Answers1

0

This is how you can access an NSArray of the different venues:

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

From there you have access to all the location data. I would create a new model class for these locations with a CLLocation property to store the longitude and latitude as well as a distance property. Iterate through the array and instantiate the models while also calculating the distance from the device's current location:

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]]];

This should give you an array of locations sorted by distance from the device which you can now display in a table.

psobko
  • 1,548
  • 1
  • 14
  • 24