//dic 转json 如果用系统自带的会出现空格。 + (NSString *)returnJSONStringWithDictionary:(NSDictionary *)dictionary{ //系统自带 // NSError * error; // NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error]; // NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; //自定义 NSString *jsonStr = @"{"; NSArray * keys = [dictionary allKeys]; for (NSString * key in keys) { jsonStr = [NSString stringWithFormat:@"%@\"%@\":\"%@\",",jsonStr,key,[dictionary objectForKey:key]]; } jsonStr = [NSString stringWithFormat:@"%@%@",[jsonStr substringWithRange:NSMakeRange(0, jsonStr.length-1)],@"}"]; return jsonStr; }
时间: 2024-10-09 21:35:54