0

My application returns a NSMutableData *receivedData.

I've opted for using NSJSONSerialization to parse this under the assumption that it would be easiest. I'm having extreme trouble trying to get my head around how to do it. I'm very new to Objective-C, from a Java background.

In Java I used gson to parse the JSON in to an array which I could use easily. I'm really struggling with this here.

My current code for parsing the JSON is:

NSError *e = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];

    if (!jsonArray) {
        NSLog(@"Error parsing JSON: %@", e);
    } else {
        for(NSDictionary *item in jsonArray) {
            NSLog(@"Item: %@", item);
        }
    }

As provided by somebody on the internet. This works and prints two items to NSLog. result and header. Here is how the JSON looks:

{
  "header":{
    "session":"sessionid",
    "serviceVersion":"1",
    "prefetchEnabled":true
  },
  "result":"50ce82401e826"
}

However if there is an error the JSON can also look like this:

{
  "header":{
    "session":"sessionid",
    "serviceVersion":"1",
    "prefetchEnabled":true
  },
  "fault":{
    "code":0,
    "message":"someErrorCode"
  }
}

How I want the code to work:

  1. Check if there is a "fault" object
  2. If there is, print fault.code and fault.message to NSLog
  3. If there isn't, I know that my JSON contains result instead of fault
  4. Print the value of result to NSLog

But I can't for the life of me figure out how to approach it. Can someone please give me some pointers?

  • Note that your `jsonArray` will be an `NSDictionary`, not an `NSArray`. By the way, this is quite trivial: check whether or not the object corresponding to the `fault` key is `nil`. –  Dec 17 '12 at 17:59
  • Yes, as far as I can tell from reading documentation, all objects (like `header` and `fault`) are `NSDictionary`s? Then `result` would be a `NSString`? –  Dec 17 '12 at 18:01
  • @ jordanmoore Yes, exactly. –  Dec 17 '12 at 18:04
  • NSLog prints "item" as a plain text name, with the plain text strings "result" and "header" when parsing the first json example. Do you know how I can put this in to an if statement? My attempts keep trying and failing... what I want to be able to do is `if (item == @"result") { /*do something*/ }` –  Dec 17 '12 at 18:13
  • because you fail to understand what gets converted to what and how JSON parsing works. Please read the Property list programming guide on developer.apple.com. –  Dec 17 '12 at 18:25

1 Answers1

0

your object appears to be a dictionary.

Try this out.

NSError *e = nil;
id jsonObj = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];

NSArray *jsonArray = nil;
NSDictionary *jsonDict = nil;

if ([jsonObj isKindOfClass:[NSArray class]]){
    jsonArray = (NSArray*)jsonObj;
}
else if ([jsonObj isKindOfClass:[NSDictionary class]]){
    jsonDict = (NSDictionary*)jsonObj;
}

if (jsonArray != nil) {
    // you have an array;
    for(NSDictionary *item in jsonArray) {
        NSLog(@"Item: %@", item);
    }
}
else if (jsonDict != nil){
    for (NSString *key in jsonDict.allKeys){
        NSLog(@"Key: %@ forItem: %@",key,[jsonDict valueForKey:key]);
    }
}
else {
    NSLog(@"Error: %@",e);
}
The Lazy Coder
  • 11,560
  • 4
  • 51
  • 69
  • Thank you, this code shows me perfectly how to approach what I need to do :) –  Dec 17 '12 at 18:21