0

Hi I am trying to parse a Json string as an NSArray and save certain results as strings to set permissions for different users in my app. My current code is:

NSError *jsonParsingError1 = nil;
        accountData = [NSJSONSerialization JSONObjectWithData:jsonAccount
                                                   options:NSJSONReadingMutableContainers error:&jsonParsingError1];

accountData is an NSMutableArray created in the .h file. jsonAccount is NSData created by converted an NSString

The NSLog out put for the array is;

  {
    account = "XXXX";
    companyName = XXXXX;
    id = XXXXX;
    websites =         (
                    {
            account = "XXXXX";
            accountId = XXXXX;
            anonymiseIP = 0;
            companyName = XXXXX;
            XXXX = 0;
            domains =                 (
                "XXXXX"
            );
            features =                 {
                advancedSegmentation = 1;
                attentionHeatmaps = 1;
                domains = 0;
                dotHeatmaps = 1;
                goalConversionTracking = 1;
                interactionHeatmaps = 1;
                leadInfo = 0;
                scrollHeatmaps = 1;
                timeHeatmaps = 1;
                users = 0;
                valueHeatmaps = 1;
                visitorPlayback = 1;
                visitorScoring = 1;
                visitors = 1;
            };
            fixedElementSelector = "";
            flagClicksReceived = 0;
            flagDataReceived = 0;
            flagGoalsReceived = 0;
            flagInteractionsReceived = 0;
            flagScrollsReceived = 0;
            id = XXXXX;
            interactionSelector = "";
            name = "XXXX";
            permissions =                 (
                segments,
                heatmaps,
                visitors,
                campaigns,
                support,
                globalSettings,
                websiteSettings
            );
            setCookies = 1;
            status = 1;
            statusMessage = "";
            statusString = OK;
            trialling = 1;
        }
    );
},       

When I try and create a sting from one of the results and display it in the log I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray:]: unrecognized selector sent to instance 0x6a950f0'

How can I save different keys as strings?

0

5 Answers 5

3

Looks like that your JSON is a Dictionary, not an Array.

Sign up to request clarification or add additional context in comments.

Comments

2

Your JSON is not array you should parse it like NSDictionary

Comments

2

After parsing the JSON, you should test what kind of object it returned. E.g.:

if ([accountData isKindOfClass:[NSArray class]]) {
   // handle like an array
} else if ([accountData isKindOfClass:[NSDictionary class]]) {
   // handle like a dictionary
}

Comments

2

Your JSON is a dictionary with four keys: account, companyName, id, and websites.

The key "websites" will give you an array.

You can iterate through the "websites" array, and each element is a dictionary.

Each of the dictionaries in the "websites" array has lots of keys like account, accountId, anonymiseIP and so on. Some of these keys have values that are dictionaries or arrays.

In the NSLog statement, (a, b, c) would be an array, while { a = x; b = y; c = z; } would be a dictionary.

1 Comment

Fantastic thank you so much for this! I am able to get all the values for the account, companyName, id and websites field. using this code 'NSString * result = [[accountData valueForKey:@"companyName"] componentsJoinedByString:@""]; NSLog(@"Result is %@", result);' How can I get the value of each key in the "websites" dictionary? Does this make sense?
1

Your server response is a dictionary so change accountData to dictionary

Write NSLog for below and you will get information in it

[accountData objectForKey:@"account"];
[accountData objectForKey:@"companyName"];
[accountData objectForKey:@"id"];
[[accountData objectForKey:@"websites"] count];//array
[[[accountData objectForKey:@"websites"] objectAtIndex:0]objectForKey:@"account"];
[[[accountData objectForKey:@"websites"] objectAtIndex:0]objectForKey:@"domains"];
[[[[accountData objectForKey:@"websites"] objectAtIndex:0]objectForKey:@"domains"]count]; //array
[[[[accountData objectForKey:@"websites"] objectAtIndex:0]objectForKey:@"domains"]objectAtIndex:0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.