JSON Object(如NSDictionary,NSArray)转化为JSON格式的NSString #iOS开发

 1     NSString *string = [self jsonObjectToJSONString:inputDataDic];
 2 
 3 
 4 -(NSString*)jsonObjectToJSONString:(id)object
 5 {
 6     if ([NSJSONSerialization isValidJSONObject:object]) {
 7         NSString *jsonString = nil;
 8         NSError *error;
 9         NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
10                                                            options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
11                                                              error:&error];
12         if (!jsonData) {
13             NSLog(@"Got an error: %@", error);
14         } else {
15             jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
16         }
17         return jsonString;
18     }else{
19         return nil;
20     }
21     
22 }

 

posted on 2015-12-03 14:58  HowMacist  阅读(327)  评论(0编辑  收藏  举报

导航