在进行JSON处理的时候,按照其数据结构的定义,对其对应的字典进行操作取值即可,
对于嵌套的结构,数组等,抽象出相应的方法,一层层的去处理(责任链模式)就可以,这样很清晰
以下是一个factory, 根据给定的对象生产出对应的JSON字符串
采用递归的方法访问一个对象,讲其内容写到一个字典中
支持的成员变量类型:
1,NSString *
2,NSArray *
3,由1,2,构成的自定义类
+ (NSMutableDictionary *) getDicFromObject: (id) theObject
{
if ([theObjectisKindOfClass:[NSStringclass]])
{
return theObject;
}
NSMutableDictionary * tmpDic = [[[NSMutableDictionaryalloc] init] autorelease];
if (theObject ==nil)
{
return tmpDic;
}
//get all the property fields names
NSString *className =NSStringFromClass([theObject class]);
const char *cClassName = [className UTF8String];
id theClass = objc_getClass(cClassName);
unsignedint outCount, i;
objc_property_t *properties = class_copyPropertyList(theClass, &outCount);
NSMutableArray *propertyNames = [[NSMutableArrayalloc] initWithCapacity:1];
for (i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
NSString *propertyNameString = [[NSStringalloc] initWithCString:property_getName(property)
encoding:NSUTF8StringEncoding];
[propertyNamesaddObject:propertyNameString];
[propertyNameStringrelease];
NSLog(@"%s %s\n",property_getName(property), property_getAttributes(property));
}
for (NSString *keyin propertyNames)
{
SEL selector = NSSelectorFromString(key);
id value = [theObject performSelector:selector];
if (value == nil)
{
value = [NSNullnull];
[tmpDicsetObject:value forKey:key];
continue;
}
//[finalDict setObject:value forKey:key];
if ([valueisKindOfClass:[NSStringclass]])
{
[tmpDicsetObject:value forKey:key];
}
elseif ([value isKindOfClass:[NSArrayclass]])
{
NSMutableArray * valueArray = [[[NSMutableArrayalloc] init]autorelease];
NSArray * aArray = (NSArray *)value;
for (int i =0; i < [aArray count]; i++)
{
[valueArrayaddObject:[NetRequestManagergetDicFromObject:[aArray objectAtIndex:i]]];
//[tmpDic setObject:[NetRequestManager getDicFromObject:[aArray objectAtIndex:i]]
// forKey:key];
}
[tmpDicsetObject:valueArray forKey:key];
}
else
{
[tmpDicsetObject:[NetRequestManagergetDicFromObject:value] forKey:key];
}
}
return tmpDic;
}
"PageSize":2 , 使用NSNumber类型来处理
NSNumber *
{"RstMode":"1","AddrBookVersion":"5154812","PageStartIndex":"3","PageSize":2}
2012-06-27 09:57:59.364 appTest[20741:10a03] primaryEmail T@"NSString",&,N,VprimaryEmail
2012-06-27 09:57:59.365 appTest[20741:10a03] primaryMobile T@"NSString",&,N,VprimaryMobile
2012-06-27 09:57:59.365 appTest[20741:10a03] name T@"NameObj",&,N,Vname
2012-06-27 09:57:59.366 appTest[20741:10a03] lastName T@"NSString",&,N,VlastName
2012-06-27 09:57:59.367 appTest[20741:10a03] middleName T@"NSString",&,N,VmiddleName
2012-06-27 09:57:59.367 appTest[20741:10a03] firstName T@"NSString",&,N,VfirstName
2012-06-27 09:57:59.368 appTest[20741:10a03] {"primaryEmail":"America","primaryMobile":null,"name":{"firstName":null,"middleName":null,"lastName":null}}
2012-06-27 09:57:59.369 appTest[20741:10a03] contactListIDs T@"NSMutableArray",&,N,VcontactListIDs
2012-06-27 09:57:59.369 appTest[20741:10a03] contactID T@"NSString",&,N,VcontactID
2012-06-27 09:57:59.370 appTest[20741:10a03] contactBaseInfo T@"ContactBaseInfoObj",&,N,VcontactBaseInfo
2012-06-27 09:57:59.370 appTest[20741:10a03] primaryEmail T@"NSString",&,N,VprimaryEmail
2012-06-27 09:57:59.371 appTest[20741:10a03] primaryMobile T@"NSString",&,N,VprimaryMobile
2012-06-27 09:57:59.371 appTest[20741:10a03] name T@"NameObj",&,N,Vname
2012-06-27 09:57:59.372 appTest[20741:10a03] lastName T@"NSString",&,N,VlastName
2012-06-27 09:57:59.372 appTest[20741:10a03] middleName T@"NSString",&,N,VmiddleName
2012-06-27 09:57:59.373 appTest[20741:10a03] firstName T@"NSString",&,N,VfirstName
2012-06-27 09:57:59.373 appTest[20741:10a03] {"contactID":"123456","contactBaseInfo":{"primaryEmail":"America","primaryMobile":null,"name":{"firstName":null,"middleName":null,"lastName":null}},"contactListIDs":[]}
2012-06-27 09:57:59.374 appTest[20741:10a03] test ={"RstMode":1,"AddrBookVersion":"test","PageStartIndex":3,"PageSize":2}
2012-06-27 09:57:59.375 appTest[20741:10a03] the class is [NSDecimalNumber]
2012-06-27 09:57:59.375 appTest[20741:10a03] the int value is 2
2012-06-27 09:57:59.375 appTest[20741:10a03] tv is NSNumber class
{"RstMode":"1","AddrBookVersion":"5154812","PageStartIndex":"3","PageSize":2}
通过解析得到的dic中,PageSize是NSDecimalNumber NSNumber类型的,处理的时候需要注意
2012-06-27 13:36:51.393 appTest[23182:10a03] {"primaryEmail":"America","primaryMobile":null,"name":{"firstName":null,"middleName":null,"lastName":null}}
(gdb) po aDic
{
name = {
firstName = "<null>";
lastName = "<null>";
middleName = "<null>";
};
primaryEmail = America;
primaryMobile = "<null>";
}
(gdb) n
(gdb) po tnull
<null>
(gdb) po [tnull class]
NSNull
可以使用[NSNull null] 来处理空值