iOS 배열과 사전 변환 json 문자열

1415 단어
+ (NSString *)dictionaryToJSONString:(NSDictionary *)dictionary 

{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"
" withString:@""]; // NSString *jsonResult = [jsonTemp stringByReplacingOccurrencesOfString:@" " withString:@""]; return jsonString; } + (NSString *)arrayToJSONString:(NSArray *)array { NSError *error = nil; // NSMutableArray *muArray = [NSMutableArray array]; // for (NSString *userId in array) { // [muArray addObject:[NSString stringWithFormat:@"\"%@\"", userId]]; // } NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; // NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"
" withString:@""]; // NSString *jsonResult = [jsonTemp stringByReplacingOccurrencesOfString:@" " withString:@""]; // NSLog(@"json array is: %@", jsonResult); return jsonString; }

좋은 웹페이지 즐겨찾기