AFNetworking JSON序列化问题(AFNetworking JSON serializa

2019-10-29 16:50发布

我送使用AFNetworking JSON(最初存储在一个NSDictionary)。 我的目标看起来像这样(从文档注释拍摄):

/**
 *  Sends a create request to the API server
 *  Success will be a dictionary containing:
 *
 *   playlistSession: {
 *       "mediaSegments": {},
 *       "mediaSequence": 0,
 *       "timeElapsed": 0,
 *       "config": {
 *           "maxSegments": 4,
 *           "targetDuration": 10
 *       },
 *       "meta": {
 *           "id": "test",
 *           "shouldBeAvailable": false,
 *           "isAvailable": false,
 *           "shouldFinish": false,
 *           "isFinished": false
 *       }
 *   }
 *
 *  And should be appended to the sessionData dictionary
 */

我得到这个服务器上:

{ fileSequence: '3',
  playlistSession: 
   { config: { maxSegments: '4', targetDuration: '10' },
     mediaSequence: '0',
     meta: 
      { id: 'MioeXvdiwB',
        isAvailable: '0',
        isFinished: '0',
        shouldBeAvailable: '0',
        shouldFinish: '0' },
     timeElapsed: '0' } }

随着字符和字符串,其中数字和布尔值应该是。 难道我做错了什么?

这里的请求(该对象被存储在一个NSMutableDictionary ):

self.sessionData[fileSequenceKey] = [NSNumber numberWithInt:fileNumber];
self.sessionData[playlistSessionKey][metaKey][shouldFinishKey] = [NSNumber numberWithBool:lastSegment];

NSString *urlString = [[NSURL URLWithString:[NSString stringWithFormat:kAppendPath, self.postPath] relativeToURL:self.manager.baseURL] absoluteString];

NSURLRequest *request = [self.manager.requestSerializer multipartFormRequestWithMethod:@"POST"
                                                                                     URLString:urlString
                                                                                    parameters:self.sessionData
                                                                     constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                                         NSError *error;
                                                                         [formData appendPartWithFileURL:target name:mediaSegmentKey error:&error];
                                                                     }];

        AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request
                                                                                  success:[self successBlock:lastSegment]
                                                                                  failure:[self failureBlock:lastSegment]];
        [operation setUploadProgressBlock:[self completionBlock]];

        [self.manager.operationQueue addOperation:operation];
        fileNumber++;

Answer 1:

我最终加入到我的方法NSDictionary+JSON称为类JSONString

/**
 *  Serializes a JSON string to be sent over the network
 *
 *  @return The serialized playlist session JSON string
 */
- (NSString*)JSONString {
    NSError *error;
    NSData *serializedData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];

    NSString *JSONString = [[NSString alloc] initWithData:serializedData encoding:NSUTF8StringEncoding];

    return JSONString;
}

返回一个NSString并不需要被强制或解析,只是一个简单的调用JSON.parse()nodejs



文章来源: AFNetworking JSON serialization problems