您的位置:首页 > Web前端 > JavaScript

关于JSON排序的相关操作

2014-03-08 17:46 435 查看
关于JSON排序的相关操作

利用array的【array allvalue】去重
1.获取到json 并且转化为Dic

/**

* 最初的数据,由json直接转过来

*/

collectionDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

2.由于这次的结构还有一个key作为统帅,所以可以将它转存为array

/**

* 将里面的实体取出【题目实体】

*/

collectionArray = [collectionDic objectForKey:@"collectionjson"];

3.本次数据填充的表格有缩放,所以需要针对section、cell进行数据填充,为了将取得的实体中的section的个数,需要将json中的

testPaperId这个键值取出,并对它进行去重。得到section的个数

/**

* 计算网络数据中testpaperID不相同的个数

*/

NSMutableDictionary *use = [[NSMutableDictionary alloc]init];

for (NSDictionary *dictemp in collectionArray) {

NSString *key = dictemp[kUC_TESTPAPERID];

[use setValue:dictemp forKey:key];

}

/**

* 可以将值一样的项除去,利用allkeys这个方法,可以将一样的值除去。

*/

NSArray *keyArray = [use allKeys];

[use objectForKey:keyArray];

/**

* 得到精简过后的数组sortCollectionArray

*/

sortCollectionArray = [use allValues];

4.利用遍历,确定testpaperId相同的cell的个数。这个i的值就是所需要的cell的个数

/**

* 根据sortCollectionArray的顺序展现列表

*/

dic = sortCollectionArray[section];

NSInteger i = 0 ;

for (NSDictionary *temp in collectionArray) {

/**

* 计算有相同testpaperId的具体项目数

*/

if(dic[kUC_TESTPAPERID] == temp[kUC_TESTPAPERID])

{

i++;

}

}

5.通过遍历取出cell的具体value

/**

* 根据sortCollectionArray的顺序进行遍历,将testpaperID相同的项目组合成array

*/

dic = sortCollectionArray[indexPath.section];

NSMutableArray *tempArray = [[NSMutableArray alloc]init];

[tempArray addObject:dic];

for (NSDictionary *temp in collectionArray) {

if(dic[kUC_TESTPAPERID] == temp[kUC_TESTPAPERID])

{

[tempArray addObject:temp];

}

}

NSDictionary *tempDic = [[NSDictionary alloc]init];

tempDic = tempArray[indexPath.row];

cell.detailLabel.text =tempDic[kUC_QUESTION];

6.得到section的title

dic = sortCollectionArray[section];

label.text = [dic objectForKey:kUC_EXAMINATIONPAPERNAME];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: