gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:24:21 24 4
gpt4 key购买 nike

我陷入了这个问题,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/

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