【问题标题】:getting error as -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance收到错误为 -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance
【发布时间】:2016-05-16 10:16:34
【问题描述】:
I have to fetch the reqid and appprimaycontact from ssoSummaryVOMap part in the json the structure of json is  below . 

在获取 json 时出现错误

 2016-05-16 15:18:48.360 Sip[30390:317309] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x7f7fc2e2e060
    2016-05-16 15:18:48.377 Sip[30390:317309] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x7f7fc2e2e060' 

我的json数据是

 json{
        appExIntgRequestId = 0;
        appQuestionnaireStatus = "E1 Pending";
        description = "Questionnaire retrieved successfully";
        isAdminRole = 0;
        isSuperAdminRole = 0;
        logoutUrl = "https://ssointrad.dev.ipc.us.aexp.com/SSOI/request?request_type=un_logoff&SSOURL=";
        quesSubmitType = 0;
        requestDeleted = 0;
        returnCode = 0;
        saveBeforeLogoff = 0;
        ssoQuestionnaireGeneralInfoVO =     {
            aimId = 200109736;
            appPrimaryContactCountryCode = 1;
            appPrimaryContactEmail = "ssoipAppUser@aexp.com";
            appPrimaryContactName = "ssoipApp User";
            appPrimaryContactPhone = 2232323123;
            appPrimaryContactPhone1 = 223;
            appPrimaryContactPhone2 = 232;
            appPrimaryContactPhone3 = 3123;
            appSecondaryContactCountryCode = "<null>";
            appSecondaryContactEcn = "<null>";
            appSecondaryContactEmail = "ssoipnonadmin@aexp.com";
            appSecondaryContactName = "ssoip NonAdmin";
            appSecondaryContactPhone = "<null>";
            appSecondaryContactPhone1 = "<null>";
            appSecondaryContactPhone2 = "<null>";
            appSecondaryContactPhone3 = "<null>";
            applicationName = "App Dir ITGC";
            engagementNo = 1264281;
            projMasterDC = PTY123567Y;
            requestId = 41458;
            ssoProjManagerEmail = "ssoip.Admin5@aexp.com";
            ssoProjManagerName = "ssoip Admin5";
            ssoTechLeadEmail = "ssoip.Admin4@aexp.com";
            ssoTechLeadName = "ssoip Admin4";
        };
        ssoQuestionnaireSpecificDetailsVO =     {
            appAjaxCalls = 0;
            appAliasName = "SIP_R6PerfTesting_Internet_51206";
            appAuthnTypeId = "-1";
            appBranding = 0;
            appChangeDetails = "";
            appChangeReasonId = "-1";
            appCoarseAuthorization = "-1";
            appCustomPages = 0;
            appDependencyAimIdList =         {
            };
            appDependencyAll = "<null>";
            appDependencyid = 0;
            appDomainId = 2;
            appE1ImplDate = "05/23/2016";
            appE2ImplDate = "07/11/2016";
            appE3ImplDate = "08/09/2016";
            appExclusiveRegistration = 0;
            appExistingRegAimIdAll = "<null>";
            appExistingRegServiceIdAll = "<null>";
            appExistingRegistration = 0;
            appHostingenvironmentId = 1;
            appInternalHostingEnvId = 1;
            appLocaleAll = "en_US|";
            appLocaleList =         (
                "en_US"
            );
            appProviderType = "-1";
            appRATypeId = 4;
            appReleaseEnvironmentE1 = 2;
            appReleaseEnvironmentE2 = 3;
            appReleaseEnvironmentE3 = 4;
            appSSOIntegrated = 0;
            appSamlCompatible = "-1";
            appSamlVersion = 0;
            appServersProvisioned = 0;
            appServiceIdAimIdList1 = "<null>";
            appServiceIdAimIdList2 = "<null>";
            appSsoPassParameters = 1;
            appUserDomainId = 2;
            appUserOrigination = 1;
            appUserTypeAll = "<null>";
            appUserTypeList =         (
            );
            appWebServerBitCompilation = "-1";
            appWebServerOS = Others;
            appWebServerOSBitCompilation = "-1";
            appWebServerType = "<null>";
            appaimidserviceid =         {
            };
        };
        ssoSummaryVOMap =     {
            41178 =         {
                aimId = 0;
                aliasName = "Alias_test_783_new_Testing";
                appName = "<null>";
                **appPrimaryContact = "<null>";**
                domainName = IntErnet;
                engagementNo = "<null>";
                hostingEnv = "Internally Hosted(Amex)";
                reqDate = "<null>";
                **reqId = 41178;**
                ssoProjManager = "<null>";
                ssoTechLead = "<null>";
                status = "<null>";
            };
            41227 =         {
                aimId = 0;
                aliasName = "Alias_new_testing_243721";
                appName = "<null>";
                **appPrimaryContact = "<null>";**
                domainName = IntrAnet;
                engagementNo = "<null>";
                hostingEnv = "Internally Hosted(Amex)";
                reqDate = "<null>";
                **reqId = 41227;**
                ssoProjManager = "<null>";
                ssoTechLead = "<null>";
                status = "<null>";
            };
            41259 =         {
                aimId = 0;
                aliasName = "Alias_test_848_testing";
                appName = "<null>";
                **appPrimaryContact = "<null>";**
                domainName = IntrAnet;
                engagementNo = "<null>";
                hostingEnv = "Internally Hosted(Amex)";
                reqDate = "<null>";
                **reqId = 41259;**
                ssoProjManager = "<null>";
                ssoTechLead = "<null>";
                status = "<null>";
            };
            41302 =         {
                aimId = 0;
                aliasName = "Alias_new_testing_4563";
                appName = "<null>";
                appPrimaryContact = "<null>";
                domainName = IntrAnet;
                engagementNo = "<null>";
                hostingEnv = "Internally Hosted(Amex)";
                reqDate = "<null>";
                reqId = 41302;
                ssoProjManager = "<null>";
                ssoTechLead = "<null>";
                status = "<null>";
            };
        };
        status = "PROC_EXEC_SUCCESS";
    }


    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"Did Finish");
        // Do something with responseData



        NSString *strData=[[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding];


        NSData *jsonData = [strData dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
        NSLog(@" json%@", json);
        NSArray* info = [json objectForKey:@"ssoSummaryVOMap"];
           NSLog(@" ssoSummaryVOMap%@", info);

        for(int i=0; i<info.count; i++){
            NSMutableDictionary * requestId= [[NSMutableDictionary alloc]init];
            requestId = [info objectAtIndex:i];


        }

        NSLog(@" the name is %@ ", _appPrimaryContact);
        NSLog(@" the reqid is %@", _reqId);
        NSLog(@" the status is %@", _status);




     i tried everything to get to the solution but no luck . 
     Please help . 

【问题讨论】:

  • NSArray* info = [json objectForKey:@"ssoSummaryVOMap"]; NSLog(@"ssoSummaryVOMap%@", info);你能把上面的NSLog的详细信息发过来吗
  • ssoSummaryVOMap 的内容是字典,但在您的代码中,您将它们视为数组。将它们视为字典,您的代码应该可以工作,
  • 感谢您的建议。我也尝试过使用 NSdictionary,但仍然得到相同的响应。

标签: ios objective-c json nsdictionary


【解决方案1】:

尝试下一个:

NSDictionjary *yourJSON = [json objectForKey:@"ssoSummaryVOMap"];
id object = [yourJSON objectForKey:@"41178"];

【讨论】:

  • 感谢您的建议。但我已经迭代 json 以获取请求 id(如 41178),因为它可能每次都不同。从那个 id 我必须选择主要联系人状态字段。
  • @Rv1190,您可以使用块迭代 NSDictionary:[defaultSettings enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { }];您的“键”将是“41227”、“41302”等,您的值是每个键的内容。
【解决方案2】:

您可以使用代码轻松获取值..

// ssoSummaryVOMap key denotes a parent dictionary 
NSDictionjary *yourJSON = [json objectForKey:@"ssoSummaryVOMap"];
// same 41178 key also denotes a dictionary which is sub dict of previous  
NSDictionjary *subDict = [yourJSON objectForKey:@"41178"];
// you can easily get the particular value now
NSString *value = [subDict objectForKey:@"aliasName"];
NSLog(@"response = %@",value);

快乐编码..

【讨论】:

  • 感谢您的建议。但我不能硬编码 41178 值,因为它每次都不同。我必须遍历数组以获取每个索引值,然后对于该索引我必须获取键值对。
  • 如果 你可以迭代你的父 dict 键而不是每次都输入 41178 的话,这里没有什么可以硬编码的 .. 在这里用户只能给出自己的想法,你可以转换它通过自己的努力有用..
【解决方案3】:
NSArray *array = @[@{@"Key1" : @"Value1"}, @{@"Key2" : @"Value2"}, @{@"Key3" : @"Value3"}];
for (NSDictionary *dict in array) {
    NSLog(@"%@", dict.allKeys);
    for (NSString *key in dict.keyEnumerator) {
        NSLog(@"Key: %@, Value %@", key, dict[key]);
    }
}

【讨论】:

    【解决方案4】:

    重写这些行

    NSArray* info = [json objectForKey:@"ssoSummaryVOMap"];
    NSLog(@" ssoSummaryVOMap%@", info);
    
    for(int i=0; i<info.count; i++)
    {
        NSMutableDictionary * requestId= [[NSMutableDictionary alloc]init];
        requestId = [info objectAtIndex:i];
    }
    

    变成这样……

    NSDictionary* info = [json objectForKey:@"ssoSummaryVOMap"];
    NSLog(@" ssoSummaryVOMap%@", info);
    
    for(int i=0; i<info.count; i++)
    {
        NSMutableDictionary * requestId = [((NSDictionary *)(info.allValues[i])) mutableCopy];
    }
    

    请注意,您确实应该添加一些意义检查代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多