从IOS中的NSDicary生成JSON字符串我有一个dictionary我需要生成一个JSON string用dictionary..可以转换吗?你们能帮忙吗?
                    
                    
                3 回答
                            慕码人2483693
                            
                                
                            
                        
                        
                                                
                    TA贡献1860条经验 获得超9个赞
@interface NSDictionary (BVJSONString)-(NSString*) bv_jsonStringWithPrettyPrint:(BOOL) prettyPrint;@end
@implementation NSDictionary (BVJSONString)
  -(NSString*) bv_jsonStringWithPrettyPrint:(BOOL) prettyPrint {
     NSError *error;
     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                   options:(NSJSONWritingOptions)    (prettyPrint ? NSJSONWritingPrettyPrinted : 0)
                                                     error:&error];
     if (! jsonData) {
        NSLog(@"%s: error: %@", __func__, error.localizedDescription);
        return @"{}";
     } else {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
     } 
 }@end@interface NSArray (BVJSONString)- (NSString *)bv_jsonStringWithPrettyPrint:(BOOL)prettyPrint;@end
@implementation NSArray (BVJSONString)-(NSString*) bv_jsonStringWithPrettyPrint:(BOOL) prettyPrint {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:(NSJSONWritingOptions) (prettyPrint ? NSJSONWritingPrettyPrinted : 0)
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"%s: error: %@", __func__, error.localizedDescription);
        return @"[]";
    } else {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }}@end- 3 回答
 - 0 关注
 - 830 浏览
 
添加回答
举报
0/150
	提交
		取消
	