我需要將json傳遞給POST參數。 輸入PARAMS是這樣的:解析json數據
{ "type":"facebook",
"friends":[{ "id": "facebook id", "avatar": "url to avatar image", "first_name" : "first_name", "last_name" :"last_name"}] }
服務器響應錯誤狀態這是一個錯誤的請求參數
Error - code: 400, message: {"status":"bad request","message":"undefined method `each' for #<String:0x00000005473e48>"}
,當我在「朋友」的部分準備JSON數據這可能是錯誤的。你能幫我審查嗎?
這裏是我的代碼 -
{
NSMutableArray *aFriends = [[NSMutableArray alloc] init];
int nCount = [[self savedSelIndexPath] count];
for (int i = 0; i < nCount && nCount > 0; i++)
{
NSIndexPath *path = [[self savedSelIndexPath] objectAtIndex:i];
NSDictionary *data = nil;
NSString *key = [[self keysArray] objectAtIndex:path.section];
NSArray *nameSection = [[self listContentDict] objectForKey:key];
data = [nameSection objectAtIndex:[path row]];
NSDictionary *dictFriends = [NSDictionary dictionaryWithObjectsAndKeys:
[data objectForKey:@"id"], @"id",
[data objectForKey:@"picture"], @"avatar",
[data objectForKey:@"first_name"], @"first_name",
[data objectForKey:@"last_name"], @"last_name",
nil];
[aFriends addObject:dictFriends];
dictFriends = nil;
}
DLog(@"aFriends: %@", aFriends);
NSDictionary *teamProfile = [[Global sharedGlobal] getPlistFile];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"facebook", @"type",
aFriends, @"friends", nil];
DLog(@"params: %@", params);
NSString *sPath = [NSString stringWithFormat:@"https://stackoverflow.com/users/%@/friends", [teamProfile valueForKey:@"userId"]];
NSMutableURLRequest *request = [[[Global sharedGlobal] httpClient] requestWithMethod:@"POST" path:sPath parameters:params];
DLog(@"request: %@", request);
….
[aFriends release];
}
感謝您的提示。我忘了告訴我正在使用SBJSON&AFNetworking。將httpClient中的參數編碼設置爲AFJSONParameterEncoding可修復我的問題 – 2012-01-30 14:07:16