0

JSONパーサーで以下を解析しようとしています。いくつかの方法を試しましたが、実際の JSON シリアライゼーション NSJSONSerialization は常にエラーをスローします (実際には NSArray *termArray = [dataDictionary objectForKey:@"term"]; でスローされますが、その理由は、dataDictionary に正しい応答がないためです。 . URL リクエストが機能しているように見えるので、なぜだろうと思います。

'NSInvalidArgumentException'、理由: '-[__NSCFArray objectForKey:]: 認識されないセレクターがインスタンスに送信されました

[
{
    "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"
}

]

これは私が解析するために使用するコードです

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

1 に答える 1

0

JSON の先頭に があります[。これは、JSON が配列であることを意味します。したがって、辞書としてアクセスすることはできません。そのため、ログには配列であると表示され、例外が発生します。

于 2013-08-03T07:56:47.530 に答える