0

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? I am using the AFNetworking library and the responseObject returns the following:

(
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?

(
    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.'
4
  • What format do you want the dictionary to have? What are your objects? What are your keys? Commented Apr 22, 2016 at 10:02
  • what is the value of query? And what does the responseObject print? By the look of your first print of responseObject it looks like a multidimensional array. Commented Apr 22, 2016 at 10:27
  • responseObject prints the first codeblock in my post above. Value of the query is the inserted text. The JSON file looks like: completion.amazon.com/search/… Commented Apr 22, 2016 at 10:32
  • What the key and value you want in dictionary ? Commented Apr 22, 2016 at 10:38

1 Answer 1

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"}]},{},{},{},{},{},{},{},{},{}],[]] is not a dictionary. 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];

Sign up to request clarification or add additional context in comments.

1 Comment

You saved the day! Thanks a million! Of course it should be the second object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.