I found this part of code in a tutorial for parsing json objects:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
// convert to JSON
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
// show all values
for(id key in res) {
id value = [res objectForKey:key];
NSString *keyAsString = (NSString *)key;
NSString *valueAsString = (NSString *)value;
NSLog(@"key: %@", keyAsString);
NSLog(@"value: %@", valueAsString);
}
// extract specific value...
NSArray *results = [res objectForKey:@"results"];
for (NSDictionary *result in results) {
NSString *icon = [result objectForKey:@"icon"];
NSLog(@"icon: %@", icon);
}
}
I was able to use that code and parse data when my JSON object was of that form:
{"data_1":0,"data_2":0,"Plato 1":0,"data_3":0....} and I was just searching for specific keys, named data_1 and so on.
Now I am fetching data over DB so my JSON object is an array.
JSON object is of that form:
[{"0":"1","id":"1","1":"text1","image":"text2","2":"0"}]
What changes do I have to make?
Your example is simply wrong. For the general case you should receive the parse result into an NSObject pointer, then test its type with
isKindOfClass. What you’re getting back is not an NSDictionary but rather an NSArray containing one entry that is an NSDictionary.