繁体   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