簡體   English   中英

解析json數據

[英]parse json data

我需要將json傳遞給POST參數。 輸入參數如下所示:

{ "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>"}

當我在“ friends”部分准備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:@"/users/%@/friends", [teamProfile valueForKey:@"userId"]];

NSMutableURLRequest *request = [[[Global sharedGlobal] httpClient] requestWithMethod:@"POST" path:sPath parameters:params]; 
DLog(@"request: %@", request);  
….
[aFriends release]; 
    }

如果要將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];

我不確定我是否理解您的要求,但是您說您需要將數據編碼為JSON,並且看不到任何用於參數的JSON編碼指令。 您只是通過了NSDictionary * dictFriends ...是否正確?

再見!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM