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 回答