1. 程式人生 > >iOS 去除JSON裡的轉義字元

iOS 去除JSON裡的轉義字元

//iOS 去除JSON裡的轉義字元

//    首先將返回格式設為二進位制格式

    manager.responseSerializer = [AFHTTPResponseSerializerserializer];


//    其次第一種方法去除轉義字元

NSString * str = [[NSStringalloc]initWithData:responseObjectencoding:NSUTF8StringEncoding];

        NSMutableString * responseString = [NSMutableStringstringWithString

:str];

             NSString *character = nil;

            for (int i =0; i < responseString.length; i ++) {

                    character = [responseString substringWithRange:NSMakeRange(i,1)];

                    if ([character isEqualToString:@"\\"])

                            [responseString deleteCharactersInRange

:NSMakeRange(i,2)];

                 }

NSData * data = [responseStringdataUsingEncoding:NSUTF8StringEncoding];

        NSDictionary * dataDic = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];

//    第二種方法去除轉義字元

        NSString * str = [[NSStringalloc

] initWithData:responseObjectencoding:NSUTF8StringEncoding];

        str = [str stringByReplacingOccurrencesOfString:@"\r\n"withString:@""];

        str = [str stringByReplacingOccurrencesOfString:@"\n"withString:@""];

        str = [str stringByReplacingOccurrencesOfString:@"\t"withString:@""];

//        str = [str stringByReplacingOccurrencesOfString:@"\\" withString:@""];

NSData * data = [strdataUsingEncoding:NSUTF8StringEncoding];

NSDictionary * dataDic = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];