用JsonModel我走了很多弯路
使用JsonModel先将JsonModel引入到ProJect里面
对应Json数据格式简历模型
例如:URL:http://semantictec.com/tec/api/mm/category?source=sears&agent=123&client=ios
新建UserModel继承自JsonModel,在.h文件里面定义属性
@property (nonatomic ,copy) NSArray *FlashData;
@property (nonatomic ,copy) NSArray *TypeCommendData;
建立FlashItem继承自JsonModel,在.h文件里面定义属性
@property (nonatomic ,copy) NSString *Name;
@property (nonatomic ,copy) NSString *Pic;
@property (nonatomic ,copy) NSString *Type;
@property (nonatomic ,copy) NSString *Url;
建立TypeCommendData继承自Jsonmodel,在.h文件里面定义属性
这里只简单的解析几个数据
@property (nonatomic ,copy) NSString *TypeName;
@property (nonatomic ,copy) NSMutableArray *VideoDatas;
@property (nonatomic ,copy) NSMutableArray *PartDatas;
@property (nonatomic ,copy) NSMutableArray *SpecialDatas;
然后在ViewController的.m文件中的ViewDidLoad里面实现下面方法
UserModel *userModel = [[UserModel alloc] initFromURLWithString:test completion:^(UserModel *model,JSONModelError *err)
{
for (i=0; i<model.FlashData.count; i++) {
NSMutableDictionary *dict111 = [model.FlashData objectAtIndex:i];
// NSLog(@"%@",dict111);
flashItem = [[FlashItem alloc] initWithDictionary:dict111 error:nil];
NSLog(@"----%@",flashItem.Pic);
}
for (i=0; i<model.TypeCommendData.count; i++) {
NSMutableDictionary *dict222 = [model.TypeCommendData objectAtIndex:i];
typeCommendItem = [[TypeCommendData alloc] initWithDictionary:dict222 error:nil];
NSLog(@"****%@",typeCommendItem.VideoDatas);
NSLog(@"&&&&&%@",typeCommendItem.PartDatas);
}
}];
这里指简单的实现了几个方法
关于JsonModel里面还有好多方法
再JsonModel里面还能通过下面i 这个方法将Model数据转化称JsonMOdel的字符串
NSString *str111 = [userModel toJSONString ];
对于已下载下来的数据也可以通过
JsonModel中的
initwithString initwithDiction
来解析
对与数组与字典的遍历以下做几点说明
一、数组遍历
第一种遍历的方法
for(int i = 0; i < array.count; i++)
{
//如果知道数组里存的对象如本题则可以用NSString接收,如果不知道则可以用id接收
//id obj = [array objectAtIndex:i];
NSString* str = [array objectAtIndex:i];
NSLog(@"%@", str);
}
//第二种变量方法:快速枚举
for(NSString* obj in array)
{
NSLog(@"%@", obj);
}
二、字典的遍历
一:字典的基本概念
Foundation中的字典(NSDictionary,NSMutableDictionary)是由键-值对组成的数据集合.正如,我们在字典里查找单词的定义一样.
通过key(键),查找的对应的value(值),key通常是字符串对象,也可以是其他任意类型对象.在一个字典对象中,key的值必须是唯一的.
此外,字典对象的键和值不可以为空(nil),如果需要在字典中加入一个空值,可以加入NSNull对象
二:不可变字典-NSDictionary
1:初始化(以一个元素和多个元素进行初始化):
//以一个键值对象 进行初始化
1
2
3
4
5
|
NSDictionary *dict1=[NSDictionary dictionaryWithObject:numObj1 forKey:@
"key1"
];
NSLog(@
"%@"
,dict1);
//以多个元素进行初始化
NSDictionary *dict2=[NSDictionary dictionaryWithObjectsAndKeys:numObj1, @
"key1"
,numObj2,@
"key2"
, nil];
NSLog(@
"%@"
,dict2);
|
//获取字典的数量
NSInteger count=[dict2 count];
NSLog(@"%ld",count);
3:根据key获取相应的value对象
1
2
|
NSString *value=[dict2 objectForKey:@
"key1"
];
NSLog(@
"%@"
,value);
|
NSEnumerator *enumerator=[dict2 keyEnumerator];
5:获取所有的key集合
1
2
|
NSArray *keys= [dict2 allKeys];
NSLog(@
"%@"
,keys);
|
1
2
|
NSArray *values=[dict2 allValues];
NSLog(@
"%@"
,values);
|
1:初始化字典
1
2
|
//初始化字典
NSMutableDictionary *mutableDic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@
"v1"
,@
"k1"
,@
"v2"
,@
"k2"
,@
"v3"
,@
"k3"
,@
"v4"
,@
"k4"
,@
"v5"
,@
"k5"
, nil];
|
1
2
3
|
NSDictionary *dict4=[NSDictionary dictionaryWithObject:@
"v6"
forKey:@
"k6"
];
[mutableDic addEntriesFromDictionary:dict4];
NSLog(@
"%@"
,mutableDic)
|
1
2
|
//向字典中添加value和key
[mutableDic setValue:@
"object"
forKey:@
"key"
];
|
1
2
3
4
|
//创建空的字典,然后设置新的一个字典,把会之前的字典全部覆盖掉
NSMutableDictionary *mutableDict2=[NSMutableDictionary dictionary];
[mutableDict2 setDictionary:mutableDic];
NSLog(@
"%@"
,mutableDict2);
|
1
2
3
|
//移除指定key的value值
[mutableDict2 removeObjectForKey:@
"k4"
];
NSLog(@
"%@"
,mutableDict2);
|
1
2
3
4
|
//删除一组key的value值
NSArray *arrayKeys=[NSArray arrayWithObjects:@
"k1"
,@
"k2"
,@
"k3"
, nil];
[mutableDict2 removeObjectsForKeys:arrayKeys];
NSLog(@
"%@"
,mutableDict2);
|
//删除字典中所有的数据
1
2
|
[mutableDict2 removeAllObjects];
NSLog(@
"%@"
,mutableDict2);
|
1:一般遍历
1
2
3
4
5
6
|
NSArray *allKeys=[mutableDic allKeys];
for
(NSInteger i=
0
; i<[allKeys count]; i++) {
NSString *obj=[mutableDic objectForKey:[allKeys objectAtIndex:i]];
NSLog(@
"%@"
,obj);
}
|
2:快速枚举
1
2
3
4
|
for
(id key in mutableDic) {
NSString *object=[mutableDic objectForKey:key];
NSLog(@
"%@"
,object);
}
|
1
2
3
4
5
6
7
8
|
NSEnumerator *enumerator1= [mutableDic keyEnumerator];
id key=[enumerator1 nextObject];
while
(key) {
id object=[mutableDic objectForKey:key];
NSLog(@
"%@"
,object);
key=[enumerator1 nextObject];
}
|