0

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];

}
Sharanya K M
  • 1,805
  • 4
  • 23
  • 44

2 Answers2

1

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • this is a good suggestion.. One i know whether its an array or nsdictionary.. how do i create an array by making all those key pair values a one object? – Sharanya K M Jan 29 '14 at 11:39
  • The key pairs will already be in one object - the dictionary. You create an array with `@[ theValueITested ]` or [NSArray arrayWithObject:]` – Wain Jan 29 '14 at 11:42
  • But this will not make all of it one object in an array... In the 1st case i have 2 objects in the array. In the 2nd case i want it to be one object in the array... – Sharanya K M Jan 29 '14 at 11:44
  • That is what it will do. If `valueForKey:@ orderListView"` gives you a dictionary and you add it to an array you have an array containing one object. – Wain Jan 29 '14 at 11:46
  • I have tried what you said and doesnt give me the expected result. Please check my code i have added and tell me if i have missed something – Sharanya K M Jan 29 '14 at 11:51
  • You won't get a mutable array - unless you are using a special parser. Check for `NSArray`. – Wain Jan 29 '14 at 11:53
  • Add `NSLog(@"%@", NSStringFromClass([[appDelegate.orderListJson objectForKey:@"orderListView"] class]));` - what does it log? – Wain Jan 29 '14 at 11:54
  • My bad.. i was trying handling this in a different method which was not called... Thanks.. Solved my problem :-) – Sharanya K M Jan 29 '14 at 11:58
0

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.

Premraj
  • 72,055
  • 26
  • 237
  • 180
  • 2
    Don't use exception handling for logical processing. This is very bad practice. Check the data and act accordingly. – Wain Jan 29 '14 at 11:53