美文网首页ios iOS
iOS 字典转字符串,字符串转字典

iOS 字典转字符串,字符串转字典

作者: biubiuboom | 来源:发表于2017-12-05 14:29 被阅读2939次

    字典转字符串

    #pragma mark 字典转化字符串
    +(NSString*)dictionaryToJson:(NSDictionary *)dic
    {
        NSError *parseError = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
        
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    

    字符串转字典

    + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
    {
        if (jsonString == nil) {
            return nil;
        }
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
        if(err) {
            NSLog(@"json解析失败:%@",err);
            return nil;
        }
        return dic;
    }
    

    相关文章

      网友评论

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

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