Deepak Khare
Deepak Khare

Reputation: 1

NSDictionary to NSMutableArray in objective c

How to parse the following format?

I parsed the XML and get it into NSDictionary. But not able to convert it into NSMutableArray.

Total count of NSDictionary is showing only 1 with this format.

{
    rsp = {
        photos = {
            page = 1;
            pages = 3709;
            perpage = 100;
            photo = (
                    {
                    farm = 4;
                    id = 12813766723;
                    isfamily = 0;
                    isfriend = 0;
                    ispublic = 1;
                    owner = "61930219@N02";
                    secret = f2efb7b4ac;
                    server = 3672;
                    text = "";
                    title = "House of Vans 02.26.14";
                },
                {
                farm = 8;
                id = 12802578725;
                isfamily = 0;
                isfriend = 0;
                ispublic = 1;
                owner = "91434132@N08";
                secret = faecb409a2;
                server = 7374;
                text = "";
                title = DSC03312;
            }
            );
            text = "";
            total = 370847;
        };
        stat = ok;
        text = "";
    };
}

Upvotes: 0

Views: 1067

Answers (2)

Harsh
Harsh

Reputation: 2908

You can make arrays of particular values.

Something like this

NSDictionary *dict;
NSArray *array = [dict allKeys];
array = [dict allKeysForObject:@"pass the object here you want to retrieve the arrays for"];
array = [dict allValues];

You can use the above code according to your functional requirements.

But what you have is an array of objects so you can apply a foreach and get all the values populated in a NSmutableArray

I can give you an example suppose you want to retrieve an array of owner, so what you can do is:

NSDictionary *dict;//Suppose this dictionary contain all the values

NSMutableArray *arrayObject = [[NSMutableArray alloc] init];
for (NSDictionary* dictionaryObjectWithCurrentVal in dict) {
     [arrayObject  addObject:[dictionaryObjectWithCurrentVal valueForKey:@"owner"]];
}
//you'll get an array containing all the owners

Upvotes: 1

tilo
tilo

Reputation: 14169

If you just want to get the two "photo" objects:

  1. Get the "rsp" object (i.e. a NSDictionary) from your NSDictionary
  2. Get the "photos" object (another NSDictionary) from 1.
  3. Get the "photo" object (a NSArray) from 2.

Now, you have an array of dictionaries holding the two "photo" objects.

Upvotes: 0

Related Questions