我正在为 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 代码中存在语法错误,请原谅我,我必须从多个函数中收集代码。
谢谢你。