目前正致力于从我返回的JSON数据中创建新的NSMutableDictionary
。我想提取:tide.tideSummary.data.type
和tide.tideSummary.date.epoch
- 我想用这两个键创建一个新的字典数组。
以下是我现在的情况:
for (NSDictionary *dict in self.tideSummary) {
NSString *type = [self.tideSummary valueForKeyPath:@"data.type"];
NSString *date = [self.tideSummary valueForKeyPath:@"date.epoch"];
[sunCycleDictionary setValue:(type) forKey:@"type"];
[sunCycleDictionary setValue:(date) forKey:@"epoch"];
}
现在我的NSLog
完全返回每个键。截至目前的示例日志,不正确
epoch = (
1388724761,
1388746063,
...
我的目标是制作此输出: - 一系列词典。
{
type: Sunrise,
epoch: 1336116677
},
{
type: Sunset,
epoch: 1336116677
},
{
type: Moonset,
epoch: 1336116677
},
...
我认为我非常接近,我只需要在他们自己的对象中加入这两个键值对。 我需要为之前创建的词典创建NSMutableArray
和addObject
吗?
我正在使用的数据:
"tideSummary": [
{
"date": {
"pretty": "11:58 AM PST on December 19, 2013",
...
"epoch": "1387483136"
},
"utcdate": {
"pretty": "7:58 PM GMT on December 19, 2013",
...
"epoch": "1387483136"
},
"data": {
"height": "5.97 ft",
"type": "High Tide"
}
},
关于如何实现这一目标的想法?
答案 0 :(得分:2)
你非常接近你的输出,但显示的输出不完全字典它是一个字典数组,所以你需要做这样的事情。
NSMutableArray *finalOutput = [[NSMutableArray alloc] init]; //Add this
for (NSDictionary *dict in self.tideSummary)
{
NSMutableDictionary *sunCycleDictionary = [[NSMutableDictionary alloc] init]; //Add this
NSString *type = [self.tideSummary valueForKeyPath:@"data.type"];
NSString *date = [self.tideSummary valueForKeyPath:@"date.epoch"];
[sunCycleDictionary setValue:(type) forKey:@"type"];
[sunCycleDictionary setValue:(date) forKey:@"epoch"];
[finalOutput addObject: sunCycleDictionary]; //Add this
}
NSLog(@"out of loop: %@", finalOutput);
答案 1 :(得分:1)
试试这个
NSMutableArray *outputArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in self.tideSummary)
{
NSMutableDictionary *sunCycleDictionary = [[NSMutableDictionary alloc] init];
NSString *type = [dict valueForKeyPath:@"data.type"];
NSString *date = [dict valueForKeyPath:@"date.epoch"];
[sunCycleDictionary setValue:(type) forKey:@"type"];
[sunCycleDictionary setValue:(date) forKey:@"epoch"];
[outputArray addObject:sunCycleDictionary];
}
NSLog(@"output array : %@", outputArray);
答案 2 :(得分:0)
Try This
NSMutableArray *temp =[[NSMutableArray alloc]init];
for (NSDictionary *dict in self.tideSummary) {
NSString *type = [self.tideSummary valueForKeyPath:@"data.type"];
NSString *date = [self.tideSummary valueForKeyPath:@"date.epoch"];
NSLog(@"type: %@", type);
[sunCycleDictionary setValue:(type) forKey:@"type"];
[sunCycleDictionary setValue:(date) forKey:@"epoch"];
[temp addObject: sunCycleDictionary];
NSLog(@"in loop: %@", temp);
}
答案 3 :(得分:0)
int i=0;
while (i<[_arayResjoBID count]) {
NSLog(@"%d",i);
NSDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setValue:[[_arayResjoBID objectAtIndex:i]valueForKeyPath:@"BarCode"] forKey:@"barcode"];
[dict setValue:[[_arayResjoBID objectAtIndex:i] valueForKeyPath:@"ItemName"] forKey:@"itemname"];
[dict setValue:[[_arayResjoBID objectAtIndex:i] valueForKeyPath:@"Quantity"] forKey:@"quantity"];
[dict setValue:@"NO" forKey:@"Yes"];
[_arayFilter insertObject:dict atIndex:i];
i++;
}
}
NSLog(@"%@",_arayFilter);
[self.tableView reloadData];
OUTPUT:在调试区域 ( { 是=否; 条形码= 1002690094467; itemname =“Box Oversized 100X80x80”; 数量= 5; }, { 是=否; 条形码= 1002690094474; itemname =“道路案例大”; 数量= 2; }, { 是=否; 条形码= 1002690094481; itemname =“Road Case New Double”; 数量= 4; } )