美文网首页
iOS 字典转json字符串

iOS 字典转json字符串

作者: 程序媛Miss_S | 来源:发表于2018-02-01 20:50 被阅读0次

    -(NSString *)convertToJsonData:(NSDictionary *)dict

    {

        NSError *error;

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

        NSString *jsonString;

        if (!jsonData) {

            NSLog(@"%@",error);

        }else{

            jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

        }

        NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];

        NSRange range = {0,jsonString.length};

        //去掉字符串中的空格

        [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

        NSRange range2 = {0,mutStr.length};

        //去掉字符串中的换行符

        [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

        return mutStr;

    }

    相关文章

      网友评论

          本文标题:iOS 字典转json字符串

          本文链接:https://www.haomeiwen.com/subject/vosjzxtx.html