У меня есть ответ JSON, который выглядит следующим образом:JSON и IOS 7 ошибок
{"load_count":171,"play_count":142,"play_rate":0.9292035398230089,"hours_watched":2.795013611111111,"engagement":0.708595,"visitors":113}
Когда я пытаюсь установить переменные значения здесь:
- (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary {
if(self = [self init]) {
// Assign all properties with keyed values from the dictionary
_plays = [jsonDictionary objectForKey:@"load_count"];
_hoursWatched = [jsonDictionary objectForKey:@"hours_watched"];
_engagement = [jsonDictionary objectForKey:@"engagement"];
}
return self;
}
я эта ошибка:
2014-02-04 11:16:37.180 BluGiant2[21192:1303] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x10a1176d0
2014-02-04 11:16:37.181 BluGiant2[21192:1303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x10a1176d0'
так что это basica что я не могу найти «load_count», хотя он есть. Является ли проблема, что это не объект, поскольку нет [] вокруг JSON?
Это только вторая попытка загрузки JSON для меня, а другая работает, единственная разница, которую я вижу, - это недостающее [].
Вот где я это называю:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"https://%s:%[email protected]/v1/stats/medias/c3e4797d8f.json", "api", "1b75e458de33a9b3f99d33f6bf409a7e145c570a"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSMutableArray *videoDetail = [[NSMutableArray alloc] init];
// Get an array of dictionaries with the key "locations"
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// Iterate through the array of dictionaries
for(NSDictionary *dict in array) {
// Create a new Location object for each one and initialise it with information in the dictionary
VideoDetail *videoD = [[VideoDetail alloc] initWithJSONDictionary:dict];
// Add the Location object to the array
[videoDetail addObject:videoD];
_textPlays.text = [NSNumberFormatter localizedStringFromNumber:videoD.plays numberStyle:NSNumberFormatterNoStyle];
}
}
Вы разбираете строку вообще или просто передаете строку JSON этому методу? –
Как вы вызываете '- (id) initWithJSONDictionary: (NSDictionary *) jsonDictionary'? –
Сначала нужно разобрать JSON. –