Reputation: 1810
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
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
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
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