【问题标题】:Parse JSON from an Array into NSDictionary? (ObjC)将数组中的 JSON 解析为 NSDictionary? (对象)
【发布时间】:2016-04-22 09:54:35
【问题描述】:

现在看了一段时间,尽我所知尝试了所有可能的方法,但仍然没有成功。

如何解析以下 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.'

【问题讨论】:

  • 您希望字典采用什么格式?你的对象是什么?你的钥匙是什么?
  • query 的值是多少? responseObject 打印什么?从你第一次打印responseObject 看,它看起来像一个多维数组。
  • responseObject 打印我上面帖子中的第一个代码块。查询的值是插入的文本。 JSON 文件如下所示:completion.amazon.com/search/…
  • 你想要字典中的键和值是什么?

标签: objective-c json nsarray nsdictionary afnetworking


【解决方案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];

【讨论】:

  • 你拯救了这一天!太感谢了!当然应该是第二个对象。
猜你喜欢
  • 2012-05-09
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2014-12-02
  • 2020-05-02
  • 1970-01-01
相关资源
最近更新 更多