gpt4 book ai didi

ios - Objective-C Json 获取和解析数据

转载 作者:行者123 更新时间:2023-11-29 01:45:18 25 4
gpt4 key购买 nike

我正在尝试从我的 api 解析数据。我的代码是正确的。我用了很多时间。但我没有收到任何这样的错误。我做错了什么。你能帮我吗?

-(void)getStatu {
NSURL *urlPath = [NSURL URLWithString:@"http://myurl.com/m/cc/cc_today.php"];
NSData *jsonData = [NSData dataWithContentsOfURL:urlPath];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);
_ccStatus = [NSMutableArray array];
NSArray *statuArray = [dataDictionary objectEnumerator];
for (NSDictionary *statuDictionary in statuArray) {
CCStatu *status = [CCStatu statuWithTitle:[statuDictionary objectForKey:@"gelen"]];
status.cevap = [statuDictionary objectForKey:@"cevap"];
status.cort = [statuDictionary valueForKeyPath:@"cort"];
status.kayip = [statuDictionary valueForKeyPath:@"kayip"];
status.lort = [statuDictionary valueForKeyPath:@"lort"];
status.tekil = [statuDictionary valueForKey:@"tekil"];
[_ccStatus addObject:status];
}

}

这是我的 json

2015-08-13 23:54:41.362 CSGB[3215:209292] {
cevap = "37,627";
cort = "00:00:48";
gelen = "54,247";
kayip = "16,620";
lort = "00:01:17";
sl = "46.30 %";
tekil = "3,316";
}

这是错误

2015-08-13 23:54:58.330 APP[3215:209292] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fcc12451c30
2015-08-13 23:54:58.336 APP[3215:209292] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fcc12451c30'

最佳答案

您的代码迭代数据不正确。你只有一本字典。没有什么可迭代的。

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);
_ccStatus = [NSMutableArray array];
CCStatus *status = [CCStatu statuWithTitle:dataDictionary[@"gelen"]];
status.cevap = dataDictionary[@"cevap"];
status.cort = dataDictionary[@"cort"];
status.kayip = dataDictionary[@"kayip"];
status.lort = dataDictionary[@"lort"];
status.tekil = dataDictionary[@"tekil"];
[_ccStatus addObject:status];

关于ios - Objective-C Json 获取和解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998679/

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