gpt4 book ai didi

ios - AFNetworking JSON responseObject 已修改格式

转载 作者:行者123 更新时间:2023-11-28 18:58:46 25 4
gpt4 key购买 nike

我正在使用 AFNetworking 获取 JSON 对象:

-(void)getData{

NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
NSURL *filePath = [NSURL fileURLWithPathComponents:pathComponents];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/service.php"
parameters:parameters

constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"audio" error:nil];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"Success: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"Error: %@", error);
}];
}

service.php 页面返回一个有效的 JSON 对象:

 {"song_id": 2, "song_name": "Red Fang - Wires", "confidence": 120, "offset_seconds": 50.3873, "match_time": 1.0025339126586914, "offset": 1085}

带有 JSON header :

header('Content-type: application/json');

但是这里是 responseObject 的输出:

Success: {
confidence = 120;
"match_time" = "1.035004138946533";
offset = 1085;
"offset_seconds" = "50.3873";
"song_id" = 2;
"song_name" = "Red Fang - Wires";
}

顺序改变了,一些引号被去掉了。每当我尝试使用 responseObject 的值(“song_name”的 execpt)时,我的应用程序都会崩溃并显示

 -[__NSCFNumber length]: unrecognized selector sent to instance 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance

我做错了什么?

最佳答案

在您的 JSON 对象中,除了 song_name 之外的所有内容都是 NSNumber(它不理解选择器“长度”)。您可以使用选择器“stringValue”将 NSNumber 转换为 NSString。

像这样:

NSString* song_id_str = [responseObject[@"song_id"] stringValue];

或者你只是使用 NSNumber

NSNumber* song_id = responseObject[@"song_id"];

关于ios - AFNetworking JSON responseObject 已修改格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28645867/

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