objective-c - 在 Objective-C 中解析嵌套的 JSON 对象

标签 objective-c ios xcode json nsmutablearray

也许是我自己想多了,或者是自己糊涂了,但我的脑袋被这个问题困住了,无法突破。

我有一个 JSON 格式:(在 http://jsonformatter.curiousconcept.com/ 验证)

{
  id: 1,
  sections: "5",
  total: "10",
  result: {
    3: 00PM: [
      {
        name: "Anurag",
        status: "Web"
      },
      {
        name: "Anurag2",
        status: "Web2"
      }
    ],
    5: 00PM: [
      {
        name: "Anurag",
        status: "Seated"
      }
    ],
    6: 00PM: [
      {
        name: "Anurag4",
        status: "Web4"
      },
      {
        name: "Anurag5",
        status: "Web5"
      },
      {
        name: "Anurag6",
        status: "Web6"
      },
      {
        name: "Anurag7",
        status: "Web7"
      }
    ]
  }
}

到目前为止我有这个:

        NSDictionary *dict = [response JSONValue];
        NSDictionary *results = [dict objectForKey:@"result"];

        NSInteger num_results = [[dict valueForKey:@"total"] intValue];
        NSInteger num_sections = [[dict valueForKey:@"sections"] intValue];

        NSMutableArray *sections = [[NSMutableArray alloc] initWithCapacity:num_sections];
        NSMutableArray *objarr = [[NSMutableArray alloc] initWithCapacity:num_results];
        NSMutableArray *obj= [[NSMutableArray alloc] initWithCapacity:num_sections];
        NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:num_results];

        for (NSString* key in results) {
            NSLog(@"Key: %@", key); // prints out 3:00 PM...5:00 PM etc...
            [obj addObject:[results objectForKey:key]]; // nested objects within each key are saved in the array
            NSLog(@"Object 1: %@", obj);
        }

        for (int i = 0; i < [obj count]; i++) {
            //NSLog(@"Object 2: %@", [obj objectAtIndex:i]);
            [temp addObject:[obj objectAtIndex:i]]; // I take each object from previous array and save it in a temp array
            for (int i = 0; i < num_results; i++) {
                NSLog(@"Object 3: %@", [temp objectAtIndex:i]);
                **[objarr addObject:[temp objectAtIndex:i]]; // I want to extract the object within the object but cannot get it to work**
            }   
        }

我能够在结果中的 3 个键中的每一个中创建一个对象数组。但是我无法将其中的每个对象作为一个单独的对象并将它们保存在一个数组中。

例如,在一个 NSArray 中我有:

Object 3: (
        {
        name = "Anurag ";
        status = Web;
    },
        {
        name = "Anurag ";
        status = Web;
    }
)

如何获取此对象中的两个对象并将它们都保存在一个数组中? 我想主要问题是我无法引用键名来获取单个对象。

最佳答案

你可以使用下面的代码,

NSDictionary *json = [response JSONValue];

// Get the objects you want
NSArray *items = [json valueForKeyPath:@"result.6: 00PM"];

这将返回一个 NSArray,其中包含键 6: 00PM

的对象

请检查这个link也是。

关于objective-c - 在 Objective-C 中解析嵌套的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11368921/

相关文章:

objective-c - 设置 NSToolbarItem 未选中

ios - 这个委托(delegate)属性是否被强引用?

iphone - 在 iOS 5.0 上使用 NSURLIsExcludedFromBackupKey 而不会崩溃

iphone - 线程核心数据

ios - 我的标签栏 Controller 委托(delegate)出了什么问题?

iphone - UIPickerView 通过数据源连接时导致泄漏

ios - 如何使用 inappSettings 设置 2 个不同的设置页面

ios - UIButton 的 IBAction 导致无法识别的选择器发送到实例错误(iOS)

ios - 我可以强制 Siri 以不同的方式发音我的应用程序名称吗?

iphone - 通过 iOS 应用程序发布到用户墙时的隐私设置