Search code examples
iphoneobjective-cxcodeyajl

Parsing JSON data error


I have the following JSON data:

(
    3,
        {
        body = "123";
        date = 1333023644;
        mid = 12;
        "read_state" = 0;
    },
        {
        body = ":)";
        date = 1332968570;
        mid = 4;
        "read_state" = 1;
    },
        {
        body = "1234";
        date = 1331844024;
        mid = 1;
        "read_state" = 1;
    }
)

And I want to get an array of body values with this code:

NSArray *array = [dialogsDictionary objectForKey:@"body"];

But I always get this error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6b65a60'


Solution

  • Consider this JSON string.

      {
       "arrayOfData": [
        {
            "body": "123",
            "date": 1333023644,
            "mid": 12,
            "read_state": 0
        },
        {
            "body": ": )",
            "date": 1332968570,
            "mid": 4,
            "read_state": 1
        },
        {
            "body": "1234",
            "date": 1331844024,
            "mid": 1,
            "read_state": 1
        }
        ]
      }
    

    This is a valid JSON string. You can check validity using http://jsonlint.com/

    Suppose you have your JSON data in variable jsonData which is of type NSData. Using SBJSON, you can parse your JSON like

      NSDictionary *jsonDictionary = [jsonData JSONValue];
    
      NSArray *array = [jsonDictionary objectForKey:@"arrayOfData"];
    
      NSMutableArray *result = [[NSMutableArray alloc]init];
      for(NSDictionary *dict in array){
          [result addObject:[dict objectForKey:@"body"]];
      }
    

    After this operation all the values for key body would be in result array.

    If the '3' was array count, you might have included that value in JSON as,

    {
    "numberOfElementsInArray": 3,
    "arrayOfData": [
        {
            "body": "123",
            "date": 1333023644,
            "mid": 12,
            "read_state": 0
        },
        {
            "body": ": )",
            "date": 1332968570,
            "mid": 4,
            "read_state": 1
        },
        {
            "body": "1234",
            "date": 1331844024,
            "mid": 1,
            "read_state": 1
        }
    ]
    }
    

    See this tutorial about JSON. You must have read this website to understand JSON

    Added by Hot Licks

    What you fail to understand is that the original JSON was essentially this:

    [
        3,
        {
            "body": "123",
            "date": 1333023644,
            "mid": 12,
            "read_state": 0
        },
        {
            "body": ": )",
            "date": 1332968570,
            "mid": 4,
            "read_state": 1
        },
        {
            "body": "1234",
            "date": 1331844024,
            "mid": 1,
            "read_state": 1
        }
    ]
    

    It was formatted differently because it was an NSLog object dump, not the original JSON source. Perfectly legitimate JSON.