-1

How can I store these three dictionaries in an object and those three objects store in an array? how can I retrive this data from array?

    [    
            {
                    "gender_desc" = Male;
                    "gender_id" = 1;
                    "gender_isactive" = 1;
            },
            {
                    "gender_desc" = Female;
                    "gender_id" = 2;
                    "gender_isactive" = 1;
            },
            {
                    "gender_desc" = Other;
                    "gender_id" = 3;
                    "gender_isactive" = 1;
            }
]
Vishal Patel
  • 511
  • 6
  • 21
  • i want to store gender_desc,gender_id and gender_isactive in one object . same as remaining other and this all object store into array. after that how to retrive this info form array. – Vishal Patel Dec 11 '15 at 19:20
  • A dictionary is an object, so you could just create three dictionaries and store those in an array, or you could create a struct or object. It looks like this may be JSON, so are you asking how you can parse the JSON into data structures? – Paulw11 Dec 11 '15 at 19:41
  • Yes, how can I parse json in data structure – Vishal Patel Dec 12 '15 at 07:00

2 Answers2

1

Using this,

NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

you can parse your json data into dictionaries. First of all this json structure is wrong. Json should be like :

{
  "gender_detail":[
  {
        "gender_desc" = Male;
        "gender_id" = 1;
        "gender_isactive" = 1;
  },
  {
        "gender_desc" = Female;
        "gender_id" = 2;
        "gender_isactive" = 1;
  },
  {
        "gender_desc" = Other;
        "gender_id" = 3;
        "gender_isactive" = 1;
  }
]
}

Then create a mutable array and store these values :

NSMutableArray *dataArray = [[NSMutableArray alloc] init];
[dataArray addObjectsFromArray:[dictionary valueForKey:@"gender_details"]];

After this array is ready. Now suppose you want gender_desc value of first object.

NSString *gender_desc = [[dataArray objectAtIndex:0] valueForKey:@"gender_desc"];

Do likewise for other details.

Vikas Mishra
  • 246
  • 1
  • 12
0

So you want to parse JSON into NSDictionaries, NSArrays and the like? The are many libraries that will happily accommodate this task, or Apple's docs can help you roll your own. Looking on SO, here just one place to start, Convert JSON feed to NSDictionary

Community
  • 1
  • 1
Justin Zealand
  • 196
  • 2
  • 7