I have managed to get a list of locations from my webserver using JSON and have the file grabbed from the app.
I know it can get the etc as I have printed it too the NSLog and it appears in JSON in the log window.
My next step is getting these values into an array so I can eventually display them into a UIPickerView.
Anybody any suggestions on how I can best get them into an array from the current JSON file?
Key is ‘name’.
I can’t for the life of me get it to loop through and add to array; maybe I am doing it wrong?
JSON data looks like:
[{"Name":"Aberdaron"},{"Name":"Aberdeen"},{"Name":"Aberdovey"},{"Name":"Aberporth"},{"Name":"Aberystwyth"},{"Name":"Albert-Bridge"},{"Name":"Aldeburgh"},{"Name":"Allington-Lock"},{"Name":"Alloa"},{"Name":"Amble"},{"Name":"Amlwch"},{"Name":"Annan-Waterfoot"},{"Name":"Anstruther-Easter"},{"Name":"Applecross"},{"Name":"Appledore"},{"Name":"Arbroath"},{"Name":"Ardglass"},{"Name":"Ardnave-Point"},{"Name":"Ardrossan"},{"Name":"Arklow"},{"Name":"Arnside"},{"Name":"Arrochar"}]
Code that I have so far is:
NSString * urlString = [NSString stringWithFormat:@"http://localhost:8888/Locations.php"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError * error;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",json);
The first answer has given a good description of how to parse the array and put it into a native object. I have written a function which is a category on NSString that you can use below. Assuming that your array is stored in a variable called json, I would use the following code, given your example:
Now nameArray is an array of all the values corresponding to the “name” keys given in your example.