I have a WCF that returns some json data in the format of:
{
"RetrieveLocationsResult": [
{
"Address": "106 Mullaghboy Road",
"Category": "Tarmac",
"Closest_Property_Number": 106,
"ID": 33,
"Image1": 1234,
"Image2": 0,
"Image3": 0,
"Latitude": 5,
"Longitude": "-1.541902",
"Notes": "New Driveway",
"User_ID": 1
},
{
"Address": "3 drumard road",
"Category": "Tarmac",
"Closest_Property_Number": 3,
"ID": 40,
"Image1": 23421,
"Image2": 0,
"Image3": 0,
"Latitude": 4,
"Longitude": "-2.541902",
"Notes": "new gates",
"User_ID": 1
},
]
}
This can be seen here in full http://crm.fpmccann.co.uk/TemperatureWebService/iphonewebservice.svc/retrievelocations
I have a class called location with each variable made up. What I want to do is parse this json data into each location, sort of make an array of locations which are going to be placed on a map using the longitude and latitude.
My method for parsing the data so far is:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlAsString = @"http://crm.fpmccann.co.uk/TemperatureWebService/iphonewebservice.svc/retrievelocations";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
NSLog(@"error: %@", error);
NSLog(@"data: %@", data);
NSLog(@"response: %@", response);
if ([data length] >0 && error == nil)
{
NSArray *array = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
NSLog(@"Response: %@", array);
CLLocationCoordinate2D location; // coordinates of the annotation
NSMutableArray *newAnnotations = [NSMutableArray array]; // an array in which we'll save our annotations temporarily
MKPointAnnotation *newAnnotation; // the pointer to the annotation we're adding
NSMutableArray *location = [array valueForKey:@"RetrieveLocationsResult"];
NSMutableArray *longitude = [[array valueForKey:@"RetrieveLocationsResult"] valueForKey:@"Longitude"];
// NSLog(@"Response", response, data);
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];
}
Please excuse that this is in the view did load, this will be changed as soon as its working. It is creating and array called location, there is 4 objects in it but there is nothing in them. What I want to do is create a for loop and populate a class based on the results?
Create a JSON Dictionary for Retrieved results:
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSMutableArray *retrieveLocationsResultArr = [NSMutableArray arrayWithArray:[jsonDict objectForKey:@"RetrieveLocationsResult"]];
[retrieveLocationsResultArr enumerateObjectsUsingBlock:^(NSDictionary *locationDict, NSUInteger idx, BOOL *stop) {
//Create your Location Objects
NSString *addressStr = [locationDict valueForKey:@"Address"];//sample, extract all values you want in your custom objects.
}];