Baidyanath
Baidyanath

Reputation: 177

Parsing arrary of dictionaries from dictionary

After parsing a XML file the output is coming in following format in a dictionary Now I want to convert it into array of dictionaries
Can anyone suggest a proper solution

Printing description of rootDictionary:
<CFBasicHash 0x8866000 [0x1078b38]>{type = mutable dict, count = 1,
entries =>
    0 : <CFString 0x8869490 [0x1078b38]>{contents = "Data"} = <CFBasicHash 0x88695c0 [0x1078b38]>{type = mutable dict, count = 1,
entries =>
    0 : <CFString 0x8869610 [0x1078b38]>{contents = "row"} = (
        {
        Address =         {
            text = "Skt. Clemens Torv 2-6";
        };
        City =         {
            text = "Aarhus C";
        };
        Country =         {
            text = Danmark;
        };
        Description =         {
            text = Ottopiste;
        };
        Lat =         {
            text = "56.156818";
        };
        Lon =         {
            text = "10.2092532";
        };
        Name =         {
            text = Ottopisteet;
        };
        Type =         {
            text = 1;
        };
        Zip =         {
            text = 8000;
        };
    },
        {
        Address =         {
            text = "Kauppatie 66";
        };
        City =         {
            text = Kauhava;
        };
        Country =         {
            text = Finland;
        };
        Description =         {
            text = "(null)";
        };
        Lat =         {
            text = "63.1001586";
        };
        Lon =         {
            text = "23.0463634";
        };
        Name =         {
            text = I m Here;
        };
        Type =         {
            text = 2;
        };
        Zip =         {
            text = 62200;
        };
    }
)
}
}

Now the output dictionary is in above format now again I want to convert it into array of dictionaries

Actual format of XML for reference:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Data>
<row>
  <Lat>56.156818</Lat>
  <Lon>10.2092532</Lon>
  <City>Aarhus C</City>
  <Address>Skt. Clemens Torv 2-6</Address>
  <Zip>8000</Zip>
  <Country>Danmark</Country>
  <Name>Ottopisteet</Name>
  <Description>Ottopiste</Description>
  <Type>1</Type>
</row>
<row>
  <Lat>63.1001586</Lat>
  <Lon>23.0463634</Lon>
  <City>Kauhava</City>
  <Address>Kauppatie 66</Address>
  <Zip>62200</Zip>
  <Country>Finland</Country>
  <Name>I am Here</Name>
  <Description>(null)</Description>
  <Type>2</Type>
</row>
</Data>

Upvotes: 0

Views: 115

Answers (2)

Nicola Miotto
Nicola Miotto

Reputation: 3706

You could just iterate on the dictionary of rows and add each object to an array:

NSMutableArray *arrayOfDictionaries = [[NSMutableArray alloc] init];
NSDictionary *rows = [rootDictionary objectForKey:@"Data"];

[rows enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
  [arrayOfDictionaries addObject:obj];
}];

UPDATE: I'm assuming that there are just "row" entries in the "Data" dictionary. Otherwise you should perform a check on the key to si if it's a "row" before.

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

DO this:

 NSMutableArray *arrData = [[parsedValueDictionary objectForKey:@"Data"]  objectForKey:@"row"];

Upvotes: 1

Related Questions