How can I go about pulling a single rating value out of a JSON dictionary? The rating value resides only in the parent JSON dictionary (it is not nested). My code is here:
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
for (NSDictionary *diction in allDataDictionary)
{
NSString *rating = [diction objectForKey:@"rating"];
[array addObject:rating];
}
[[self myTableView] reloadData];
}
Secondly, how can I make an If statement to convert the rating value to an NSString
for it to appear on the iPhone simulator?
In order to fetch all the ratings objects from the dictionary, you can use:
NSArray *array = [allDataDictionary valueForKey:@"rating"];
This depends on the JSON representation and the structure of your data set.
For the second issue, if this object is an NSNumber
, you can try this:
if ([[array objectAtIndex:indexPath.row] isKindOfClass:[NSNumber class]]) {
cell.textLabel.text = [[array objectAtIndex:indexPath.row] stringValue];
}
Note that you have to use isKindOfClass
method to check for the class and stringValue
to convert to string.