2012-01-28 199 views
0

我需要将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]; 
    } 

回答

0

我不知道如果我明白你问什么,但你说你需要将数据编码到JSON,我不能看到你的任何参数编码JSON指令。你只是传递一个NSDictionary * dictFriends ...是否正确?

见你!

+0

感谢您的提示。我忘了告诉我正在使用SBJSON&AFNetworking。将httpClient中的参数编码设置为AFJSONParameterEncoding可修复我的问题 – 2012-01-30 14:07:16

1

如果你想JSON传递给你设置它像这样的服务器:

NSMutableURLRequest *networkRequest; 
    networkRequest = [[NSMutableURLRequest alloc] initWithURL: 
          [NSURL URLWithString:[networkServerAddress stringByAppendingString:@""]]]; 
    send = [[json objectAtIndex:i] dataUsingEncoding:NSUTF8StringEncoding]; 
    //Set the headers appropriately 
    [networkRequest setHTTPMethod:@"POST"]; 
    [networkRequest setValue:@"application/json"  
      forHTTPHeaderField: @"Content-type"]; 
    [networkRequest setValue:[NSString stringWithFormat:@"%d", [send length]] 
      forHTTPHeaderField:@"Content-length"]; 
    [networkRequest setValue:@"application/json"  
      forHTTPHeaderField:@"Accept"]; 
    //Set the body to the json encoded string 
    [networkRequest setHTTPBody:send]; 

发送必须是一个NSData的,而不是一本字典。如果你想这样做,你必须先解析字典。 JSON有很多优秀的解析器/编写器,我个人喜欢并使用SBJson。与SBJson你可以做这样的事情:

SBJsonWriter *writer = [[SBJsonWriter alloc] init]; 
NSData *dataToSend = [writer dataWithObject:data];