簡體   English   中英

如何從Rest API解析JSON響應

[英]How to parse JSON response from Rest API

我正在開發一個將解析來自Rest API的數據的應用程序。 因為我以前沒有做過,所以我想知道NSScanner或NSRange是否可以為此工作。 這是使用NSScanner達到此目的的示例。 這是一個很小的字符串,因此不需要太多代碼,因此似乎是一個快速而骯臟的選擇。 有沒有更簡便快捷的方法?

- (void)GetAliasName:(NSString *)cno selector:(NSString*)selectorName completionHandler:(void (^)(NSDictionary *, NSError *))handler

{

NSMutableDictionary *d = [[NSMutableDictionary alloc] init];

[d setValue:interface forKey:@"interface"];

[d setValue:@"GetAlias" forKey:@"method"];

NSMutableDictionary *p = [[NSMutableDictionary alloc] init];

cno = [[NSUserDefaults standardUserDefaults] objectForKey:@"cno"];

[p setValue:cno forKey:@"cno"];

[p setValue:selectorName forKey:@"selector"];

[d setValue:p forKey:@"parameters"];

NSData *data = [NSJSONSerialization dataWithJSONObject:d options:0 error:nil];

[self load:data completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    NSLog(@"done");

    NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"GetAlias RAW response = %@", s);

    if (apiConditions)
    {
        dispatch_async(dispatch_get_main_queue(),
                       ^{
                           [hud hide:YES];
                           [MBProgressHUD hideHUDForView:self.view animated:YES];
                           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No network connection or no data found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                           [alert show];
                           NSLog(@"data is NIL");
                       });


    }
    else
    {
        NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        handler(d, error);

            //scans for aliasname
            NSString *match = @"AliasName\": \"";
            NSString *preTel;
            NSString *postTel;

            NSScanner *scanner = [NSScanner scannerWithString:s];
            [scanner scanUpToString:match intoString:&preTel];

            [scanner scanString:match intoString:nil];
            postTel = [s substringFromIndex:scanner.scanLocation];


            //removes newlines, characters, and whitespaces from aliasname
            NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"}]\""];
            postTel = [[postTel componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
            postTel = [[postTel componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString: @""];


            NSLog(@"postTel: %@", postTel);
            NSString *docno = [[NSUserDefaults standardUserDefaults] objectForKey:@"docno"];

            //if docno is filled in, look up TR by docno
            if ([docno isEqual: @""])
            {

            [self GetTR:(NSString*)postTel customStatus:customStatus completionhandler:^(NSDictionary *dictionary, NSError *error) {
                nil;
            }];

}
}];
}

NSJSONSerialization目的而構建的NSJSONSerialization

如下使用它:

NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSString *aliasName = dictionary[@"AliasName"];

編輯

由於您已經使用了此字典並擁有字典,因此您唯一需要的步驟就是使用所需的鍵訪問字典(最后一步)。

暫無
暫無

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

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