gpt4 book ai didi

ios - JSON 和 iOS 7 错误

转载 作者:行者123 更新时间:2023-11-28 22:16:18 26 4
gpt4 key购买 nike

我有一个如下所示的 JSON 响应:

{"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'

所以它基本上是在告诉我它找不到 “load_count”,即使它在那里。问题是它不是一个对象,因为 JSON 周围没有 [] 吗?

这只是我第二次尝试加载 JSON,另一次成功了,我看到的唯一区别是缺少 []

这里是我的称呼:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"https://%s:%s@api.wistia.com/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 存储为字符串,而不是 NSDictionary。您需要做的是转换您的 JSON。

由于您的 JSON 是一个字符串,因此首先获取一个 NSData 对象:

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

然后使用 NSJSONSerialization 类将它变成一个 NSDictionary:

NSError* error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

此时,您将拥有一个可以使用的 NSDictionary

编辑:

看起来您正在尝试将 JSON 响应视为字典数组,但根据您在上面粘贴的 JSON 响应,您实际上只有一个字典。所以不要重复它,因为我相信这就是你遇到问题的原因。它只是迭代键,它们是字符串。

关于ios - JSON 和 iOS 7 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21559265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com