0

When i parse one JSON i got following result inside my NSArray

(
    {
    category = Allergies;
    group = Allergies;
    name = "Food Allergies";
    option = "";
    subGroup = "";
    type = VARCHAR;
},
    {
    category = Allergies;
    group = Allergies;
    name = "Drug Allergies";
    option = "";
    subGroup = "";
    type = VARCHAR;
},
    {
    category = Allergies;
    group = "";
    name = "Blood Group";
    option = "[{\"id\":1,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"A+\"},{\"id\":2,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"A-\"},{\"id\":3,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"B+\"},{\"id\":4,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"B-\"},{\"id\":5,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"O+\"},{\"id\":6,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"O-\"},{\"id\":7,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"AB+\"},{\"id\":8,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"AB-\"}]";
    subGroup = "";
    type = SINGLESELECT;
}
)

Now when i tried to take value for key "option"
[array valueForKey:@"option"]objectAtIndex:indexPath.row];

i got following result

 [{"id":1,"isDefault":false,"additionalOption":false,"optionName":"A+"},{"id":2,"isDefault":false,"additionalOption":false,"optionName":"A-"},{"id":3,"isDefault":false,"additionalOption":false,"optionName":"B+"},{"id":4,"isDefault":false,"additionalOption":false,"optionName":"B-"},{"id":5,"isDefault":false,"additionalOption":false,"optionName":"O+"},{"id":6,"isDefault":false,"additionalOption":false,"optionName":"O-"},{"id":7,"isDefault":false,"additionalOption":false,"optionName":"AB+"},{"id":8,"isDefault":false,"additionalOption":false,"optionName":"AB-"}]

but when tried to access "optionName" like following format

[optionArray valueForKey:@"optionName"]; then i got this error

 *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x10d8638a0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key optionName.'

Please help me to fix this..

1
  • try using enumeration block Commented Dec 2, 2015 at 13:53

5 Answers 5

1

Assuming you are using ios 5 or higher, JSON serialization is built in:

NSArray *json = [NSJSONSerialization 
        JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding]
        options:kNilOptions 
        error:&error];

Prior to ios5, you can use SBJSON to achieve the same:

NSArray *jsonObjects = [jsonParser objectWithString:string error:&error];
Sign up to request clarification or add additional context in comments.

Comments

0

Use this method

- (NSArray *)convertStringToDictionary:(NSString *)str{
    NSError *jsonError;
    NSData *objectData = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *json = [NSJSONSerialization JSONObjectWithData:objectData                                               options:NSJSONReadingMutableContainers error:&jsonError];

    return json;
}

And call like this

NSArray *arr =  [self convertStringToDictionary:@"[{\"id\":1,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"A+\"},{\"id\":2,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"A-\"},{\"id\":3,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"B+\"},{\"id\":4,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"B-\"},{\"id\":5,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"O+\"},{\"id\":6,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"O-\"},{\"id\":7,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"AB+\"},{\"id\":8,\"isDefault\":false,\"additionalOption\":false,\"optionName\":\"AB-\"}]"];

NSLog(@"Option Name ---->  %@",[[arr objectAtIndex:0] valueForKey:@"optionName"]);

OUTPUT

Option Name ----> A+

6 Comments

Why Swift? OP tagged the question with [objective-c].
can u make it in objc pls
@Kreiri Converted it in ObjC.
@Bangalore Please have a look on updated answer.
@Bangalore Please see updated answer which is working perfect.
|
0

Try using below got to convert to array

 NSArray *tmpDayArray=[[NSArray alloc]init];
 NSError *error;
 NSData *dictData = [NSJSONSerialization dataWithJSONObject:tmpDayArray options:NSJSONWritingPrettyPrinted error:&error];
 NSString *open_days = [[NSString alloc] initWithData:dictData encoding:NSUTF8StringEncoding];

Try Below code for convert it to NSArray

    NSArray * array =[NSJSONSerialization JSONObjectWithData:[[NSString stringWithFormat:@"%@",@"YOUR STRING"] dataUsingEncoding:NSUTF8StringEncoding]options: NSJSONReadingMutableContainers error:nil];

Comments

0

You have a complex "object graph." It looks like your outer object is an array, and inside that you have dictionaries.

If optionArray is an NSArray then it will give you exactly the error you are reporting if you try to treat it as a dictionary.

Your code:

[array valueForKey:@"option"]objectAtIndex:indexPath.row];

Is doing that (Although you are using KVC, where you should be using array and dictionary methods directly.)

Your code should look like this instead:

NSDictionary* aDict = array[indexPath.row];
NSString *optionString = aDict[@"option];

Comments

0

Without using any third party like SBJson

NSString *optionString  = [[array valueForKey:@"option"]objectAtIndex:indexPath.row];

Actually you are getting optionString as json String so you have to parse it and then use like this

NSData *data = [optionString dataUsingEncoding:NSUTF8StringEncoding];

NSArray * optionArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

NSLog(@"%@",[optionArray valueForKey:@"optionName"]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.