objective-c - 如何使用 JSON 解析一个对象中的 5 个数组

标签 objective-c ios xcode xcode4

我陷入了这个问题,JSOn 的输出是

"forecast":[ 
{"day":"Today","condition":"MostlyClear","high_temperature":"94","low_temperature":"70"},
{"day":"Tomorrow","condition":"Sunny","high_temperature":"95","low_temperature":"69"},
{"day":"Saturday","condition":"Sunny","high_temperature":"94","low_temperature":"71"},
{"day":"Sunday","condition":"Sunny","high_temperature":"93","low_temperature":"70"},
{"day":"Monday","condition":"Sunny","high_temperature":"94","low_temperature":"70"}
]

我需要条件键来显示表格 View 单元格中的值。我的代码是

temp=[[NSString alloc] initWithData:[dataLoader httpData] encoding:NSUTF8StringEncoding];
NSLog(@"Patient Details %@",temp);

array = [[temp JSONValue] objectForKey:@"forecast"];

NSDictionary* dic = [array objectAtIndex:[indexPath row]];
NSString *strDay = [dic objectForKey:@"day"];
NSString *stringCondition = [dic objectForKey:@"condition"];
NSLog(@"condition is %@", stringCondition);
NSString *stringTemp = [dic objectForKey:@"high_temperature"];
NSString *stringLow = [dic objectForKey:@"low_temperature"];

但它只显示一个条件值,我想要一次显示所有五个条件。我还尝试使用 NSArray 而不是 NSString 作为条件,但没有运气。

最佳答案

 array = [[temp JSONValue] objectForKey:@"forecast"];

在这个地方创建一个 for 循环:

for(NSUInteger index = 0; index<[array count]; index++)
{

 NSDictionary* dic = [array objectAtIndex:index];
 NSString *strDay = [dic objectForKey:@"day"];
 NSString *stringCondition = [dic objectForKey:@"condition"];
 NSLog(@"condition is %@", stringCondition);
 NSString *stringTemp = [dic objectForKey:@"high_temperature"];
 NSString *stringLow = [dic objectForKey:@"low_temperature"];
}

希望这会有所帮助...

关于objective-c - 如何使用 JSON 解析一个对象中的 5 个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560356/

相关文章:

ios - 使用哪个 Google Analytics cocoapods pod

objective-c - 如何使用 Swift 测试 Objective-C 框架的内部结构

objective-c - 什么情况下[[NSScreen mainScreen]visibleFrame]会返回null?

iphone - 构建类似 iPhone 手机的应用程序

ios 旋转器在停止命令后不停止

ios - Module-Swift.h 有编译错误

objective-c - 可旋转的UIButton

ios - removeAllObjects 崩溃应用程序

ios - 如何从我的 iOS 应用程序获取所有消息应用程序列表

ios - Ionic CLI 构建错误