0

I am trying to parse the following with my JSON parser. I tried several ways but the actual JSON Serialization NSJSONSerialization always throws the error (well actually it is thrown on the NSArray *termArray = [dataDictionary objectForKey:@"term"]; but the reason is, because the dataDictionary does not have the correct response. So I wander why, as the URL request seems to work.

'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance

[
{
    "id": 20765985,
    "url": "http://quizlet.com/20765985/basic-portuguese-flash-cards/",
    "title": "Basic Portuguese",
    "created_by": "aUser",
    "term_count": 4,
    "created_date": 1362858462,
    "modified_date": 1362858462,
    "has_images": false,
    "subjects": [
        "Portuguese"
    ],
    "visibility": "only_me",
    "editable": "only_me",
    "has_access": true,
    "description": "",
    "has_discussion": true,
    "lang_terms": "pt",
    "lang_definitions": "en",
    "creator": {
        "username": "aUser",
        "account_type": "free",
        "profile_image": "https://quizlet.com/a/i/animals/38.ndeQ.jpg",
        "id": 6602337
    },
    "terms": [
        {
            "id": 696519541,
            "term": "mesa",
            "definition": "table",
            "image": null
        },
        {
            "id": 696519542,
            "term": "amigo",
            "definition": "friend",
            "image": null
        },
        {
            "id": 696519543,
            "term": "leite",
            "definition": "milk",
            "image": null
        },
        {
            "id": 696519544,
            "term": "vender",
            "definition": "sale",
            "image": null
        }
    ],
    "display_timestamp": "In March 2013"
}

]

This is the code I use to parse

NSURL *myURL = [NSURL URLWithString:@"https://api.quizlet.com/2.0/users/<myUser>/sets?access_token=<myAccessToken>&whitespace=1"];

NSData *jsonData = [NSData dataWithContentsOfURL:myURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSArray *termArray = [dataDictionary objectForKey:@"term"];
falsetru
  • 357,413
  • 63
  • 732
  • 636
renesteg
  • 551
  • 1
  • 9
  • 24

1 Answers1

0

At the start of your JSON you have a [, that means the JSON is an array. So you can't access it as a dictionary. That is why the log says its an array and you get the exception.

Wain
  • 118,658
  • 15
  • 128
  • 151