【问题标题】:how to access JSON data and display it in XCode Logcat and if response is null i want to show alert如何访问 JSON 数据并将其显示在 XCode Logcat 中,如果响应为空,我想显示警报
【发布时间】:2017-03-03 06:58:06
【问题描述】:

以下是从 Web 获取数据的示例代码,我尝试在 logcat 中显示它。我在这里 pst 代码。请帮我解决它。提前致谢

NSArray *dicAllNetworks = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

NSLog(@"JsonData=%@",dicAllNetworks);

for (NSDictionary *dict in dicAllNetworks)
{
    NSInteger number = [dict[@"UserID"] intValue];
    NSString *UserTypeConst =dict[@"UserTypeConst"];
    NSString *name = dict[@"UserName"];
}

这是回复:

JsonData=[{"UserID":2478,"UserTypeConst":"PQR","UserName":"115005p"}]

如果响应为空

我想显示警报。

【问题讨论】:

  • 您正在访问 for 循环中的数据您还想要什么?
  • 当调试器进入 for 循环时......由于未捕获的异常@inder,它给了我终止应用程序
  • 此时它给出了这样的错误,同时发布整个异常日志
  • JsonData=[{"UserID":2478,"UserTypeConst":"PQR","UserName":"115005p"}] 2017-03-03 12:14:12.460 api[11500:120541 ] -[__NSCFString countByEnumeratingWithState:objects:count:]:无法识别的选择器发送到实例 0x60800005dee0 2017-03-03 12:14:12.534 api[11500:120541] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“ -[__NSCFString countByEnumeratingWithState:objects:count:]: 无法识别的选择器发送到实例 0x60800005dee0'
  • 您的data 是如何形成的?

标签: ios objective-c json xcode asp.net-web-api


【解决方案1】:
      dispatch_async(dispatch_get_main_queue(), ^{

                                                UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Please enter valid UserID and Password" message:@"" preferredStyle:UIAlertControllerStyleAlert];

                                                UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                                                                     {
                                                                         [alert dismissViewControllerAnimated:YES completion:nil];
                                                                     }];

                                                [alert addAction:ok];
                                                [self presentViewController:alert animated:YES completion:nil];

                                                NSLog(@"Invalide Login Credential.");


                                            });

【讨论】:

    【解决方案2】:

    试试这个

    for (NSDictionary *dict in dicAllNetworks)
    {
        NSNumber *number = [NSNumber numberWithInt:dict[@"UserID"]];
        NSString *UserTypeConst =dict[@"UserTypeConst"];
        NSString *name = dict[@"UserName"];
    }
    

    【讨论】:

    • NSNumber *number = [dict[@"UserID"]];在第一个右方括号后显示红色标记
    • 检查更新的答案尝试 NSNumber *number = [NSNumber numberWithInt:dict[@"UserID"]];
    • 它给了我错误,代码中所需的预期标识符@jignesh
    • 以未捕获的 NSException 类型异常终止 来自调试器的消息:由于信号 6 而终止
    【解决方案3】:

    试试这个:-

    for (NSDictionary *dict in dicAllNetworks){
        NSInteger number = [[dict valueForKey:@"UserID"] integerValue];
        NSString *UserTypeConst =[dict valueForKey:@"UserTypeConst"];
        NSString *name = [dict valueForKey:@"userName"];
    }
    

    【讨论】:

    • libc++abi.dylib:以未捕获的 NSException 类型异常终止 来自调试器的消息:由于信号 6 而终止
    【解决方案4】:

    问题出在你的data 请看下面的代码并阅读cmets

    //This is your actual string
    // Code 1
    // NSString *jstr = @"\"[{\\\"UserID\\\":24,\\\"UserTypeConst\\\":\\\"PAR\\\",\\\"UserNa‌​me\\\":\\\"160005p\\\"}]\"";
    
    // where as your string be like this
    // Code 2
    NSString *jstr = @"[{\"UserID\":2478,\"UserTypeConst\":\"PQR\",\"UserName\":\"115005p\"}]";
    NSData *data = [jstr dataUsingEncoding:NSUTF8StringEncoding];
    
    
    
    NSArray *dicAllNetworks = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
    NSLog(@"JsonData=%@",dicAllNetworks);
    
    for (NSDictionary *dict in dicAllNetworks)
    {
      NSInteger number = [dict[@"UserID"] intValue];
      NSString *UserTypeConst =dict[@"UserTypeConst"];
      NSString *name = dict[@"UserName"];
    }
    

    只需运行上面的示例,它就可以正常工作,但是当您评论 Code 2 并取消评论 Code 1 时,您会遇到问题。
    我不知道您是如何创建 data 对象的,但这不是有效的 json。


    在您现有的代码中,只需将代码更改如下

    NSError *error = nil;
    NSArray *dicAllNetworks = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"eror=%@", error);
    

    你可以看到你的json格式不正确

    【讨论】:

    • 嗨,它的工作谢谢你,我正在努力解决这过去的 5 个小时,非常感谢@inder kumar rathore
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多