简体   繁体   English

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

[英]Parse JSON from an Array into NSDictionary? (ObjC)

Looking at it for some time now and tried every possible way in my knowledge, but still without any success. 现在看了一段时间,就我所知尝试了所有可能的方法,但仍然没有成功。

How can I parse the following JSON to get the first array into a dictionary? 如何解析以下JSON以将第一个数组放入字典中? I am using the AFNetworking library and the responseObject returns the following: 我正在使用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";
            }
        );
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    },
            {
    }
),
    (
)

)

Now, how can I get the first array into a NSDictionary? 现在,如何将第一个数组放入NSDictionary?

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

ObjC code: 对象代码:

    - (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];
}

This will return the following error: 这将返回以下错误:

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

["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"}]},{},{},{},{},{},{},{},{},{}],[]] is not a dictionary. ["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"}]},{},{},{},{},{},{},{},{},{}],[]]不是字典。 Your response is an array. 您的回应是一个数组。 The first item is the query. 第一项是查询。 The second seems to be the thing that you want. 第二个似乎是您想要的东西。 I would try to do this for a test: 我会尝试做一个测试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM