Renuka Pandey
Renuka Pandey

Reputation: 1810

XML to JSON convert issue. I want to change it in array of dictionary structure. is it possible?

I want to change it in array of dictionary structure. is it possible? Actually I have a XML structure, while converting xml to json, incase it is having one value, it gives dictionary structure, otherwise array structure, can you please apprise how to handle/parse it.

INCASE OF ONE VALUE

            item =         {
                "ML_KEY" =             {
                    text = "test1";
                };
                name =             {
                    text = "test2";
                };
                order =             {
                    text = 1;
                };
                value =             {
                    text = "Test3"
                };
            };

INCASE OF MORE THAN ONE VALUES

 item =             (
                            {
                "ML_KEY" =                     {
                    text = "Testing4";
                };
                name =                     {
                    text = "Testing3";
                };
                order =                     {
                    text = 1;
                };
                value =                     {
                    text = "TestingB";
                };
            },
                            {
                "ML_KEY" =                     {
                    text = "New2";
                };
                name =                     {
                    text = "New";
                };
                order =                     {
                    text = 2;
                };
                value =                     {
                    text = "Preview"
                };
            },
                            {
                "ML_KEY" =                     {
                    text = "Testing2"
                };
                name =                     {
                    text = Remove;
                };
                order =                     {
                    text = 3;
                };
                value =                     {
                    text = Remove;
                };
            },
                            {
                "ML_KEY" =                     {
                    text = "Testing";
                };
                name =                     {
                    text = "E";
                };
                order =                     {
                    text = 4;
                };
                value =                     {
                    text = "D";
                };
            }
        );
    };

Upvotes: 0

Views: 73

Answers (3)

Renuka Pandey
Renuka Pandey

Reputation: 1810

Yes I did the same as above with slight changes:

    id checkDict = [tempDict valueForKeyPath:@"items.item"];
    if ([checkDict isKindOfClass:[NSDictionary class]])
    {
      // parse your dictionary here

    }else if([checkDict isKindOfClass:[NSArray class]])
    {
      // parse your array here

    }else{
}

Upvotes: 0

Sunny
Sunny

Reputation: 831

You should check for the class type of the data you are getting. You can do it like this.

id items =[[NSDictionary dictionaryWithXMLString:string] valueForKey:@"items"];
if ([items isKindOfClass:[NSDictionary class]])
{
  // parse your dictionary here

}else if([items isKindOfClass:[NSArray class]])
{
  // parse your array here

}else{

}

Upvotes: 1

Ricardo Alves
Ricardo Alves

Reputation: 652

    NSDictionary *responseDict = XMLResponse;

    for (NSDictionay * dict in responseDict){
        __do stuff here__
    }

you can also work with NSMutableDictionay, that way you can add multiple lines as you go throw the response

Upvotes: 0

Related Questions