将数组中的JSON解析为NSDictionary? (ObjC)

时间:2016-04-22 09:54:35

标签: objective-c json nsarray nsdictionary afnetworking

现在看一下,在我的知识中尝试了一切可能的方法,但仍未取得任何成功。

如何解析以下JSON以将第一个数组放入字典?我正在使用AFNetworking库,responseObject返回以下内容:

(
y,
    (
    yeti,
    "yeti tumbler",
    "yoga pants",
    "yoga mat",
    "yeezy boost 350",
    "yeti cup",
    yoga,
    yeezy,
    "young living essential oils",
    yugioh
),
    (
            {
        nodes =             (
                            {
                alias = garden;
                name = "Home & Kitchen";
            },
                            {
                alias = "outdoor-recreation";
                name = "Outdoor Recreation";
            },
                            {
                alias = "fan-shop";
                name = "Sports Fan Shop";
            }
        );
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    }
),
    (
)

现在,我怎样才能将第一个数组放入NSDictionary中?

(
    yeti,
    "yeti tumbler",
    "yoga pants",
    "yoga mat",
    "yeezy boost 350",
    "yeti cup",
    yoga,
    yeezy,
    "young living essential oils",
    yugioh
)

ObjC代码:

    - (void)requestJSONWithQuery:(NSString *)query
{
    if (!_requestOperation.isCancelled) [_requestOperation cancel];
    query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *string = [NSString stringWithFormat:_URLDictionaryForAPIs[@(_APIType)], query];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    _requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    _requestOperation.responseSerializer = [AFJSONResponseSerializer serializer];
    __block FFFGoogleSearchAutoCompleteController *selfInBlock = self;
    [_requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


     //   NSLog(@"%@", responseObject);
        selfInBlock.suggestions = [(NSDictionary *)responseObject mutableArrayValueForKey:query];
        [selfInBlock refreshSuggestionTable];


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {


    }];
    [_requestOperation start];
}

这将返回以下错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSTaggedPointerString 0xa0000000000006b1> valueForUndefinedKey:]: this class is not key value coding-compliant for the key K.'

1 个答案:

答案 0 :(得分:1)

["k",["kindle","kindle fire","kindle books","keurig","knife","kate spade","keyboard","kindle paperwhite","knife sharpener","kindle fire case"],[{"nodes":[{"name":"Kindle Store","alias":"digital-text"},{"name":"Electronics","alias":"electronics"},{"name":"Computers","alias":"computers"}]},{},{},{},{},{},{},{},{},{}],[]] 不是字典。你的回复是一个数组。第一项是查询。第二个似乎是你想要的东西。我会尝试做这个测试:

selfInBlock.suggestions = [(NSArray *)responseObject objectAtIndex:1];