我正在为iPhone编写原生应用程序,我对这个主题很新 使用AFNetworking我请求(使用POST)并处理JSON回复 但我注意到,一旦JSON响应更复杂:
{
"isFound": "YES",
"timestamp": "2013-06-12 22:46:47",
"screenTitle": "Perla Review",
"placeName": "Perla",
"placeUniqueId": "101",
"placeCategory": "PUB",
"username": "@jim",
"userImgURL": "",
"gender": "male",
"infoMsg": "TBD",
"youLike": "Like",
"likesInfoMsg": "",
"revInfoList": [
{
"type": 0,
"data": "",
"text": ""
},
{
"type": 1,
"data": "2",
"text": ""
},
{
"type": 2,
"data": "3",
"text": ""
}
],
"commentsList": []
}
然后AFNetworking无法读取和构造子数组(revInfoList& commentsList)
我做错了什么,或AFNetworking不支持这样的json结构?
这是我的客观C代码,用于请求数据并处理回复:
static NSString *const myAPIBaseURL = @"http://somedomain.com/api/";
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:myAPIBaseURL]];
NSString *reqPath = @"review/review_fullinfo";
// prepare params
NSString *reqData_loginUsername = [[[Storage_Modal alloc] init] getLoginUsername];
NSString *reqData_currentCoordinatesLat = [[NSNumber numberWithDouble:[appDelegateInst.myCurrentLocation coordinate].latitude] stringValue];
NSString *reqData_currentCoordinatesLon = [[NSNumber numberWithDouble:[appDelegateInst.myCurrentLocation coordinate].longitude] stringValue];
NSString *reqData_reviewUniqueId = reviewUniqueId;
NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:
@"json", @"format",
reqData_loginUsername, @"loginUsername",
reqData_currentCoordinatesLat, @"currentCoordinatesLatitude",
reqData_currentCoordinatesLon, @"currentCoordinatesLongitude",
reqData_reviewUniqueId, @"reviewUniqueId",
nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
[client postPath:reqPath parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *xxx = responseObject;
// 'xxx' contains all the top level keys and values, but the sub arrays are empty... why?
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"SERVER ERROR: %@", error);
}
**注意:如果Objective-C代码中存在语法错误,请原谅我,我必须从多个函数中收集代码。
谢谢。
答案 0 :(得分:1)
AFNetworking使用NSJSONSerialization将您的JSON转换为Foundation对象,通常是NSDictionary或NSArray。 NSJSONSerialization严格遵守RFC 4627。
您发布的JSON响应是有效的JSON,因此我只能为您描述的行为考虑3个原因:
xxx
NSDictionary包含的内容您可以通过查看AFJSONRequestOperation
的{{1}}和responseString
属性来测试#1。可以通过仔细检查responseData
对象来测试#2和#3。如果您在此处发布这些对象的输出,我们可以帮助您更好地进行诊断。
你的AFNetworking代码和你期望的JSON看起来都很好,所以我认为这不是AFNetworking的问题。