Reputation: 1805
I have two kinds of json formats that are getting parsed.
The data that gives no issues and when i fetch using "orderListView" key, the array gets generated fine. Array count is 2 in this case.
{
"orderListView": [
{
"status": "AC",
"totalRecords": "5",
},
{
"status": "SH",
"totalRecords": "5",
}
]
}
There's a square bracket missing in the below data and this too gets parsed into two objects where as this should have been one object similar to the above json. How do i handle this issue?
{
"orderListView": {
"status": "AC",
"totalRecords": "1",
}
}
EDIT : Tried solution
if ([[appDelegate.orderListJson objectForKey:@"orderListView"] isKindOfClass:[NSMutableArray class]]) {
orderMainArray = [appDelegate.orderListJson objectForKey:@"orderListView"];
}
else
{
NSArray *array = [NSArray arrayWithObject:[appDelegate.orderListJson objectForKey:@"orderListView"]];
orderMainArray = [array copy];
}
Upvotes: 0
Views: 110
Reputation: 74581
the problem is @ your fellow colleague side.
json should maintain one standard. either should populate into an array or to an object. the above two cases json is coming two different way.
@ your end you can manage using exception handling but, it is risky.
Upvotes: 0
Reputation: 119031
You don't 'resolve' it, you deal with it or get the source JSON changed.
To deal with it, check the Class type of the object that you get back when you request orderListView
. It will either be an NSArray
or NSDictionary
so you can test with isKindOfClass:
to decide what to do.
Upvotes: 1