gpt4 book ai didi

ios - JSON 写入类型无效(NSConcreteData)?

转载 作者:可可西里 更新时间:2023-11-01 03:05:13 25 4
gpt4 key购买 nike

我正在使用 AFNetworking 发布带有一些数据的音频。我收到以下异常。

*** 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 中。 audioNSData。作为 NSJSONSerialization documentation说:

An object that may be converted to JSON must have the following properties:

  • The top level object is an NSArray or NSDictionary.

  • All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

  • All dictionary keys are instances of NSString.

  • Numbers are not NaN or infinity.

如果要在 JSON 中包含 NSData,则必须将其转换为字符串。常用的方法是对其进行 base64 编码(然后在目标位置对 base64 字符串进行解码)。

关于ios - JSON 写入类型无效(NSConcreteData)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32155471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com