我在下面有一个json响应,需要根据'order'键对数组进行排序,并将其显示给subView。
[
{
"ProjectID": 1,
"SLNO": 1,
"ID": 1,
"Type": "Text",
"Name": "First Name",
"Order": 1,
"Flag": "F"
},
{
"ProjectID": 1,
"SLNO": 3,
"ID": 2,
"Type": "Text",
"Name": "Company",
"Order": 5,
"Flag": "F"
},
{
"ProjectID": 1,
"SLNO": 4,
"ID": 4,
"Type": "Text",
"Name": "Personal Email",
"Order": 3,
"Flag": "F"
},
{
"ProjectID": 1,
"SLNO": 2,
"ID": 8,
"Type": "Text",
"Name": "Last Name",
"Order": 2,
"Flag": "F"
}
]
我阅读this documentation但是如何在以下代码中将'tempArray'存储和排序到自定义类'SaveAsking'。我知道我需要使用initWithKey:@“Order”,但是在下面的例子中如何?
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:Nil];
//NSArray *arrayLabel = [[NSArray alloc] initWithObjects:label1, label2, label3, label4, label5, label6, nil];
NSMutableArray *arrayOrder = [NSMutableArray arrayWithCapacity:myJsonArray.count];
NSMutableArray *arrayName = [NSMutableArray arrayWithCapacity:myJsonArray.count];
i = 0;
for(NSDictionary *myJsonDictionary in myJsonArray)
{
//UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++];
//[label setText:myJsonDictionary[@"Name"]];
NSString *name = myJsonDictionary[@"Name"];
NSLog(@"Question from ws2 is %@", name);
projectIdGobal = myJsonDictionary[@"ProjectID"];
NSLog(@"Project id from ws2 is %@", projectIdGobal);
slno = myJsonDictionary[@"SLNO"];
NSLog(@"slno from ws2 is %@", slno);
NSString *idWS2 = myJsonDictionary[@"ID"];
NSLog(@"id from ws2 is %@", idWS2);
order = myJsonDictionary[@"Order"];
NSLog(@"order from ws2 is %@", order);
flag = myJsonDictionary[@"Flag"];
NSLog(@"flag from ws2 is %@", flag);
[self putLabelsInScrollView:name:order]; //need to call this method depending on the sorted 'order' key
SaveAsking *save = [[SaveAsking alloc] initWithslno:slno withOrder:order withFlag:flag];
save.slno = slno;
save.order = order;
save.flag = flag;
[temporaryArray addObject:save];
//temporaryArray = [[NSMutableArray alloc] initWithObjects:slno, order, flag, nil];
[arrayOrder addObject:order];
[arrayName addObject:name];
i++;
}
//Sort array or orders
sortedArrayOfOrder = [arrayOrder sortedArrayUsingComparator:^(id str1, id str2) {
if ([str1 integerValue] > [str2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([str1 integerValue] < [str2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(@"Array or orders = %@", arrayOrder);
NSLog(@"Sorted array of orders = %@", sortedArrayOfOrder);
//NSLog(@"Array from ws2 = %@", temporaryArray);
NSLog(@"Array in myJsonArray = %@", myJsonArray);
NSLog(@"Number of cycles in for-each = %d", i);
}
答案 0 :(得分:2)
我认为你可以通过json对象上的sortUsingComparator直接对myJsonArray进行排序。
所以
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:Nil];
NSArray sortedArrayOfOrder = [arrayOrder sortedArrayUsingComparator:^(NSDictionary json1, NSDictionary json2) {
if ([[json1 objectForKey:@"Order"] integerValue] > [[json2 objectForKey:@"Order"] integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[json1 objectForKey:@"Order"] integerValue] < [[json2 objectForKey:@"Order"] integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];