zoukankan      html  css  js  c++  java
  • iOS字典转字符串方法

    新建Object文件

    .h文件中

    +(NSString *)convertToJsonData:(NSDictionary *)dict;

    .m文件中

    +(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:@" " withString:@"" options:NSLiteralSearch range:range2];

        

        return mutStr;

        

    }

    用法:

        NSString *datastr = [WXObject convertToJsonData:dict];

  • 相关阅读:
    luogu P1833 樱花 看成混合背包
    luogu P1077 摆花 基础记数dp
    luogu P1095 守望者的逃离 经典dp
    Even Subset Sum Problem CodeForces
    Maximum White Subtree CodeForces
    Sleeping Schedule CodeForces
    Bombs CodeForces
    病毒侵袭持续中 HDU
    病毒侵袭 HDU
    Educational Codeforces Round 35 (Rated for Div. 2)
  • 原文地址:https://www.cnblogs.com/FZP5/p/8472462.html
Copyright © 2011-2022 走看看