urlEncoding在iOS上失败JSON

问题描述:

我有一个简短的URL编码NSStrings的方法。urlEncoding在iOS上失败JSON

- (NSString *)urlEncodeValue:(NSString *)strToEncode 
{ 
    NSLog(@"Testing strToEncode: %@", strToEncode); 
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)strToEncode, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8)); 
    NSLog(@"Testing strToEncode: %@", strToEncode); 
    NSLog(@"Testing urlEncode: %@", encodedString); 
    return encodedString; 
} 

NSLog行是用于调试,当我解决了这个问题时会被注释掉。

尝试使用字符串Testing URLencode % "hmm",按预期工作。

2012-11-03 06:44:02.140 FoodyU[23223:c07] Testing strToEncode: Testing URLencode % "hmm" 
2012-11-03 06:44:02.141 FoodyU[23223:c07] Testing strToEncode: Testing URLencode % "hmm" 
2012-11-03 06:44:02.142 FoodyU[23223:c07] Testing urlEncode: Testing%20URLencode%20%25%20%22hmm%22%20 

具有以下JSONarray尝试并不:

2012-11-03 06:44:02.142 FoodyU[23223:c07] Testing strToEncode: (
     { 
     dataMode = fastUpload; 
     dateCreated = "373599360.708794"; 
     dateModified = "373599414.702938"; 
     dateSynced = "373632241.82217"; 
     entityName = CommodityTypes; 
     myName = " Commodity Type"; 
     sortKey = 99; 
     username = adamekPhoneDev; 
     uuidKey = "338A0507-355F-4DF5-97A4-C0F1FF651D6F"; 
    }, 
     { 
     dataMode = fastUpload; 
     dateCreated = "373599366.851905"; 
     dateModified = "373599382.473983"; 
     dateSynced = "373632241.82217"; 
     entityName = CommodityTypes; 
     myName = "Anoth Type"; 
     sortKey = 90; 
     username = adamekPhoneDev; 
     uuidKey = "6C64E7C6-4C57-4DFC-BECA-D2AB2339E204"; 
    } 
) 
2012-11-03 06:44:02.143 FoodyU[23223:c07] -[__NSArrayM length]: unrecognized selector sent to instance 0x9946ae0 
2012-11-03 06:44:02.145 FoodyU[23223:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x9946ae0' 
*** First throw call stack: 
(0x1ffb012 0x1690e7e 0x20864bd 0x1feabbc 0x1fea94e 0x1f76090 0x1feba1d 0x284d8 0x28a52 0x251cb 0x21c18 0x219b5 0x6878d5 0x687b3d 0x108ee83 0x1fba376 0x1fb9e06 0x1fa1a82 0x1fa0f44 0x1fa0e1b 0x24267e3 0x2426668 0x5d865c 0x224d 0x2175) 
libc++abi.dylib: terminate called throwing an exception 

登录串转换工作前;它是一个格式良好的JSON字符串。然后它崩溃与从内的某个地方的错误消息NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)strToEncode, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8));

帮助?

[__NSArrayM length]: unrecognized selector sent to instance 0x9946ae0

你把一个NSArray其中NSString预期。首先将JSON数据序列化为字符串是一个好主意。

为什么你的URL编码的JSON数据,无论如何?

+0

首先,我只是回来这里说哎呀我没有序列化的阵列还和你打我吧。其次,我发布的Web应用使用x-www-form-urlencoded,JSON数据是表单的一个字段。 – adamek

尝试使用此

NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
         NULL, 
         (CFStringRef)unencodedString, 
         NULL, 
         (CFStringRef)@"!*'();:@&=+$,/?%#[]", 
         kCFStringEncodingUTF8);