iOS字符串转字典->字符串加密解密转成jso的梗(Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end)

重点在这:

NSString *str = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
#pragma mark - JSON字符串转化为字典
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    NSString *str = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
    
    return [self strToDict:str];
}
+ (NSDictionary *)strToDict:(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;
}

参考:https://www.jianshu.com/p/8efc29d3050d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

你可能感兴趣的:(iOS字符串转字典->字符串加密解密转成jso的梗(Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end))