简体   繁体   English

如何使用目标C从Json响应中获取特定值?

[英]How To Get Particular Values From Json Response Using Objective C?

I am trying to get response of first key value without mentioning key name of "A" using objective C . 我试图获得first key response ,而没有使用objective C提及键名"A" I cant get exactly, please help me to get from below response. 我无法完全了解,请帮助我从以下回复中获取帮助。

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];

NSDictionary *response = [JSON[@"response"]firstObject];


response = {
    A =     {
        company =         (
                        {
                no = "115";
                student = "Mich";
                school =                 (
                                        {
                        grade = A;

                    }
                );
                test = "<null>";
                office = tx;
            }
      );
   };
}

There are a few ways to do this, depending on your exact requirements. 有几种方法可以执行此操作,具体取决于您的确切要求。 If you just need to access the value of each key in JSON[@"response"] , you can enumerate the JSON dictionary: 如果您只需要访问JSON[@"response"]中每个键的值,则可以枚举JSON字典:

[JSON enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSDictionary *dict = (NSDictionary *)obj;

    ...

    if (shouldStop) { // whatever condition you want, if any
        *stop = YES;
    }
}];

If you want some kind of ordering, you need to use [JSON allKeys] : 如果需要某种排序,则需要使用[JSON allKeys]

NSArray *keys = [[JSON allKeys] sortedArrayUsing...];  // Use whichever sort method you like
for (NSString *key in keys) {
    NSDictionary *dict = JSON[key];

    ...
}

If all you want are the values, you can use [JSON allValues] . 如果只需要这些值,则可以使用[JSON allValues] Sort if desired. 如果需要,排序。

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

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