http://blog.csdn.net/worldzhy/article/details/49982491
将NSArray、NSDictionary转换为JSON格式进行网络传输,是经常用到的,但是iOS没有提供方便的方法库。我们模拟一个实例,假设我们需要把一个自定义的一个模型(只有若干属性的Class)的数组转为一个JSON字符串,作为后端需要的一个参数传给后端,下面给出了实现方法:
[objc] view plain copy
- //HSPictureModel数组转为json
- - (NSString *)pictureArrayToJSON:(NSArray *)picArr {
- if (picArr && picArr.count > 0) {
- NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
- for (HSPictureModel *model in picArr) {
- NSData *jsonData = [Object2Json getJSON:model options:NSJSONWritingPrettyPrinted error:nil];
- NSString *jsonText = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- [arr addObject:jsonText];
- }
- return [self objArrayToJSON:arr];
- }
- return nil;
- }
- 上方这个方法<span style="font-size:18px;">第一步,将一个model转换为JSON字符串,然后将数组中的这些字符串拼接起来,得到最终的JSON字符串。实现了我们的目的。其中用到的Object2Json的代码附在文章最下面。
- </span>
- //把多个json字符串转为一个json字符串
- - (NSString *)objArrayToJSON:(NSArray *)array {
- NSString *jsonStr = @"[";
- for (NSInteger i = 0; i < array.count; ++i) {
- if (i != 0) {
- jsonStr = [jsonStr stringByAppendingString:@","];
- }
- jsonStr = [jsonStr stringByAppendingString:array[i]];
- }
- jsonStr = [jsonStr stringByAppendingString:@"]"];
- return jsonStr;
- }
以上,我们实现了数据从客户端传到后台时可能需要的转换,下面再看一下,如何把后台传过来的JSON形式的字符串,解析为客户端认识的NSArray或者NSDictionary。
[objc] view plain copy
- //json字符串转为数组
- - (NSArray *)stringToJSON:(NSString *)jsonStr {
- if (jsonStr) {
- id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
- if (tmp) {
- if ([tmp isKindOfClass:[NSArray class]]) {
- return tmp;
- } else if([tmp isKindOfClass:[NSString class]]
- || [tmp isKindOfClass:[NSDictionary class]]) {
- return [NSArray arrayWithObject:tmp];
- } else {
- return nil;
- }
- } else {
- return nil;
- }
- } else {
- return nil;
- }
- }
这个独立的方法可以实现后端JSON字符串转为NSArray或NSDictionary的功能。
最后,附上Object2Json类的实现,该实现摘抄自网络。
[objc] view plain copy
- #import "Object2Json.h"
- #import <objc/runtime.h>
- @implementation Object2Json
- + (NSDictionary*)getObjectData:(id)obj
- {
- NSMutableDictionary *dic = [NSMutableDictionary dictionary];
- unsigned int propsCount;
- objc_property_t *props = class_copyPropertyList([obj class], &propsCount);
- for(int i = 0;i < propsCount; i++)
- {
- objc_property_t prop = props[i];
- NSString *propName = [NSString stringWithUTF8String:property_getName(prop)];
- id value = [obj valueForKey:propName];
- if(value == nil)
- {
- value = [NSNull null];
- }
- else
- {
- value = [self getObjectInternal:value];
- }
- [dic setObject:value forKey:propName];
- }
- return dic;
- }
- + (NSData*)getJSON:(id)obj options:(NSJSONWritingOptions)options error:(NSError**)error
- {
- return [NSJSONSerialization dataWithJSONObject:[self getObjectData:obj] options:options error:error];
- }
- + (id)getObjectInternal:(id)obj
- {
- if([obj isKindOfClass:[NSString class]]
- || [obj isKindOfClass:[NSNumber class]]
- || [obj isKindOfClass:[NSNull class]])
- {
- return obj;
- }
- if([obj isKindOfClass:[NSArray class]])
- {
- NSArray *objarr = obj;
- NSMutableArray *arr = [NSMutableArray arrayWithCapacity:objarr.count];
- for(int i = 0;i < objarr.count; i++)
- {
- [arr setObject:[self getObjectInternal:[objarr objectAtIndex:i]] atIndexedSubscript:i];
- }
- return arr;
- }
- if([obj isKindOfClass:[NSDictionary class]])
- {
- NSDictionary *objdic = obj;
- NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:[objdic count]];
- for(NSString *key in objdic.allKeys)
- {
- [dic setObject:[self getObjectInternal:[objdic objectForKey:key]] forKey:key];
- }
- return dic;
- }
- return [self getObjectData:obj];
- }
- @end
时间: 2024-10-09 12:27:32