-3

この JSON 文字列を解析するときに問題があります。この文字の JSON 文字列を解析する場合と、開始ブラケットと終了ブラケットがない場合のアプローチに違いはありますか?

JSON は次のようになります。

    [
    {
        "id": 66,
        "username": "simon"
    },
    {
        "id": 69,
        "username": "simon"
    },
    {
        "id": 70,
        "username": "simon"
    },
    {
        "id": 71,
        "username": "simon"
    }
]

私のコード:

    -(void)searchUserNamed:(NSString *)userID andAddTo:(UITableView*)tableView andAddUsersTo:(NSMutableArray*)users
    {     
        NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
        NSNumber *own_id = @([[NSUserDefaults standardUserDefaults] integerForKey:@"id"]);
        NSMutableDictionary *HTTPPostDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                                   userID, @"username",
                                                   token, @"token",
                                                   own_id, @"user_id",
                                                   nil];
        [[WebAPI sharedInstance] commandWithParams:HTTPPostDictionary command:@"search_user" onCompletion:^(NSDictionary *json){

                    for(NSDictionary *username in json){
                        NSLog(@"username: %@ FOUND", username);
                        [users addObject:username];
                        }
                    [tableView reloadData];
        }];
    }

**Which utilizes**

    -(void)commandWithParams:(NSMutableDictionary*)params command:(NSString *)command onCompletion:(JSONResponseBlock)completionBlock
    {

        NSString *_path = [NSString stringWithFormat:@"%@%@",self.baseURL, command];
        NSLog(@"path: %@", _path );

        NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
        NSNumber *userID = @([[NSUserDefaults standardUserDefaults] integerForKey:@"id"]);
        if(userID)[params setObject:userID forKey:@"user_id"];
        if(token)[params setObject:token forKey:@"token"];
            NSLog(@"%@",params);
        NSMutableURLRequest *apiRequest =
        [self multipartFormRequestWithMethod:@"POST"
                                        path:_path
                                  parameters:params
                   constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                       //TODO: attach file if needed
                   }];


        AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            //success!

            NSLog(@"%@",responseObject);
            if([responseObject objectForKey:@"status"] && ![[responseObject objectForKey:@"status"] isEqualToString:@"ok"] )
                ;
            else {
                NSLog(@"%@",responseObject);
                completionBlock(responseObject);
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            //failure :(
            completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"ERROR"]);
            // Unable to establish a connection to the server.
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server error"
                                                            message:@"Please try again later"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }];

       [self enqueueHTTPRequestOperation:operation];
    }
4

3 に答える 3

1
[
    {
        "id": 66,
        "username": "simon"
    },
    {
        "id": 69,
        "username": "simon"
    },
    {
        "id": 70,
        "username": "simon"
    },
    {
        "id": 71,
        "username": "simon"
    }
]

この JSON と上記の括弧なしの JSON の違いは、上記の JSON は有効であり、括弧なしでは無効な JSON であることです。

于 2013-06-20T10:12:30.313 に答える
0

ViewController.h 内

    NSString *idStr;
    NSString * usernameStr;
    NSMutableArray *dataSourceArray;
    NSMutableDictionary *MutableDictionry;

ViewController.m 内

-(void) viewDidLoad
{
    dataSourceArray =[NSMutableArray new];
    NSArray *array;//this array will contain your json
    dataSourceArray = [self usersFromArray:array];        
 }
- (NSMutableArray *)usersFromArray:(NSArray *)array
 {
     NSMutableArray *users = [NSMutableArray array];
     for (NSDictionary *dict in array) {
if (dict)
       {
              idStr = dict[@"id"];
              usernameStr = dict[@"username"];
        MutableDictionry=[NSDictionary dictionaryWithObjectsAndKeys:idStr,@"id",usernameStr,@"username",nil];

       }
    [users addObject:MutableDictionry];
 }
return users;
}
于 2013-06-20T10:09:22.533 に答える