简体   繁体   English

在Objective-C中提取递归JSON数据

[英]Extracting recursive JSON data in objective-c

I am new to Xcode and I'm using AFNetworking to fetch data from a server. 我是Xcode的新手,并且正在使用AFNetworking从服务器获取数据。

I am trying to extract following JSON data from a response using a recursion function: 我正在尝试使用递归函数从响应中提取以下JSON数据:

{
    "id": "12345",
    "version": 1,
    "name": "apple",
    "mainCategory": {
      "id": "23456",
      "version": 2,
      "name": "fruit",
      "mainCategory": {
        "id": "food",
        "version": 1,
        "name": "eat",
        "mainCategory": {
          "id": "root",
          "version": 1,
          "name": "Root",
          "mainCategory": null,
          "productCount": 1,
          "leaf": null,
          "productClass": null,
          "loaded": true
        },
        "productCount": 13,
        "leaf": null,
        "productClass": "xxx",
        "loaded": true
      },
      "productCount": 0,
      "leaf": null,
      "productClass": "xxx",
      "loaded": true
    },
    "productCount": 0,
    "leaf": "1",
    "productClass": "xxx",
    "loaded": true
  }

Below is the recursion method where the function returns the object. 下面是函数返回对象的递归方法。 The mainCategory is nested within the block mainCategory嵌套在块中

- (ProductCategory *) recursive :(NSDictionary *)productcat
{

    NSDictionary *productcategory = productcat;
    ProductCategory *p = [[ProductCategory alloc] init];
    if (![[productcategory objectForKey:@"mainCategory"]  isEqual: NULL])
    {
        [p setIdentifier:[productcategory objectForKey:@"id"]];
        [p setName:[productcategory objectForKey:@"name"]];
        [p setLeaf:[productcategory objectForKey:@"leaf"]];
        [p setProductClass:[productcategory objectForKey:@"productClass"]];
        [p setProductCount:[productcategory objectForKey:@"productCount"]];
        [p setVersion:[productcategory objectForKey:@"version"]];
        [p setMainCategory:[productcategory objectForKey:@"mainCategory"]];
        NSDictionary *mainCategory = [productcategory objectForKey:@"mainCategory"];
        [self recursive:mainCategory];

    }

    return p;

} }

How can I extract the data and stop once the depth is reached and retract? 到达深度后如何提取数据并停止并缩回?

mainCategory is again an object of ProductCategory mainCategory再次是ProductCategory的对象

ProductCategory is NSobject

@interface ProductCategory : NSObject


@property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSString *identifier;
@property (nonatomic, strong) ProductCategory *mainCategory;
@property(retain) NSNumber *productCount;
@property (retain) NSNumber *version;

@property (nonatomic, strong) NSString *leaf;
@property (nonatomic, strong) NSString *productClass;
 - (ProductCategory *) recursive :(NSDictionary *)productcat
 {
     ProductCategory *p = nil;
     if (productcat != nil) {
        p = [[ProductCategory alloc] init];
        p.identifier = productcat[@"id"];
        p.name = productcat[@"name"];
        p.leaf = productcat[@"leaf"];
        p.productClass = productcat[@"productClass"];
        p.productCount = productcat[@"productCount"];
        p.version = productcat [@"version"];
        p.mainCategory = [self recursive:productcat[@"mainCategory"]];
     }

    return p;
 }

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

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