
*** Terminating app due to uncaught exception 'NSinvalidargumentexception',reason: 'InvalID type in JsON write (NSConcreteData)'
我正在使用此代码..
dict=@{@"access_token":[defaults valueForKey:@"TOKEN"],@"email":email,@"prayer":passData};if ([NSJsONSerialization isValIDJsONObject:dict]) { NSLog(@"Proper JsON Object"); } NSError *error; NSData *JsonData = [NSJsONSerialization dataWithJsONObject:dict options:NSJsONWritingPrettyPrinted error:&error]; NSURL *URL = [NSURL URLWithString:url]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10]; [request sethttpMethod:@"POST"]; [request setValue: @"application/Json" forhttpheaderFIEld:@"Content-Type"]; [request sethttpBody:JsonData]; AFhttpRequestoperation *op = [[AFhttpRequestoperation alloc] initWithRequest:request]; op.responseSerializer = [AFJsONResponseSerializer serializerWithReadingOptions:NSJsONReadingallowFragments]; [op setCompletionBlockWithSuccess:^(AFhttpRequestoperation *operation,ID responSEObject) {} 我需要以这种格式发布值
{ "access_token" = 2cf0d8a66654fa4f; email = ""; prayer = { audio = <63616666 00010000 64657363 00000000 00000020 40e58880 00000000 696d6134 00000000 00000044 00000040 00000002 00000000 6b756b69 00000000 00000000 66726565 0000000> 98903013 31faae9c bb7b0780 80808080>; "category_ID" = ""; description = ""; "expired_date" = "Expiration Date"; "is_audio" = 1; "is_urgent" = 0; "prayer_access_ID" = ""; "prayer_type_ID" = 1; subject = ""; };} 我不知道为什么会发生这种情况,我之前用这种格式发布了值,但是当我尝试将音频作为数据发布时,我得到了这个例外.
提前致谢
编辑
我在这里制作数据
NSData *audiofile=[self audioData]; if (audiofile==nil) { [passData setobject:@"" forKey:@"audio"]; [passData setobject:@"0" forKey:@"is_audio"]; }else{ [passData setobject:audiofile forKey:@"audio"]; [passData setobject:@"1" forKey:@"is_audio"]; }-(NSData *)audioData{ NSArray *dirPaths = NSSearchPathForDirectorIEsInDomains( NSdocumentDirectory,NSUserDomainMask,YES); Nsstring *docsDir = [dirPaths objectAtIndex:0]; Nsstring *soundfilePath = [docsDir stringByAppendingPathComponent:@"recordTest.caf"]; NSURL *url = [NSURL fileURLWithPath:soundfilePath]; NSData *audioData=[NSData dataWithContentsOfURL:url]; return audioData;} 这就是我插入音频文件
解决方法 问题是你不能把NSData放在JsON中.音频是NSData.正如NSJsONSerialization documentation所说:An object that may be converted to JsON must have the following propertIEs:
The top level object is an
NSArrayorNSDictionary.All objects are instances of
Nsstring,NSNumber,NSArray,NSDictionary,orNSNull.All dictionary keys are instances of
Nsstring.Numbers are not
NaNor infinity.
如果要在JsON中包含NSData,则必须将其转换为字符串.常见的方法是对其进行base64编码(然后在目标中对base64字符串进行解码).
总结以上是内存溢出为你收集整理的ios – JSON写入中的类型无效(NSConcreteData)?全部内容,希望文章能够帮你解决ios – JSON写入中的类型无效(NSConcreteData)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)