获取递归NSDictionaries和NSArray形成JSON

时间:2014-06-02 10:33:12

标签: ios objective-c json recursion

我是Xcode的新手,我很难找到从JSON获取NSArray和NSDictionaries的解决方案。请有人帮帮我。 这是我的JSON:

[
    {
        "categoryID": "1",
        "category_parentID": "0",
        "category_name_english": "Telivision",
        "category_name_arabic": "واعتلاء ثم. حاول المضي عن أخر",
        "category_date_edited": "2013-02-16 06:55:02",
        "sub": [
            {
                "categoryID": "2",
                "category_parentID": "1",
                "category_name_english": "Sharp",
                "category_name_arabic": "واعتلاء ثم. حاول المضي عن أخر",
                "category_date_edited": "2013-02-16 06:55:02",
                "sub": []
            },
            {
                "categoryID": "5",
                "category_parentID": "1",
                "category_name_english": "LG",
                "category_name_arabic": "",
                "category_date_edited": "2014-05-22 00:00:00",
                "sub": [
                    {
                        "categoryID": "7",
                        "category_parentID": "5",
                        "category_name_english": "LG 1",
                        "category_name_arabic": "",
                        "category_date_edited": "2014-05-22 00:00:00",
                        "sub": []
                    },
                    {
                        "categoryID": "8",
                        "category_parentID": "5",
                        "category_name_english": "LG 2",
                        "category_name_arabic": "",
                        "category_date_edited": "2014-05-17 00:00:00",
                        "sub": []
                    }
                ]
            }
        ]
    },
    {
        "categoryID": "3",
        "category_parentID": "0",
        "category_name_english": "Computer",
        "category_name_arabic": "واعتلاء ثم. حاول المضي عن أخر",
        "category_date_edited": "2013-02-16 06:55:02",
        "sub": [
            {
                "categoryID": "4",
                "category_parentID": "3",
                "category_name_english": "HP COLOUR",
                "category_name_arabic": "واعتلاء ثم. حاول المضي عن أخر",
                "category_date_edited": "2013-02-16 06:55:02",
                "sub": []
            },
            {
                "categoryID": "6",
                "category_parentID": "3",
                "category_name_english": "dell",
                "category_name_arabic": "",
                "category_date_edited": "2014-05-16 00:00:00",
                "sub": [
                    {
                        "categoryID": "10",
                        "category_parentID": "6",
                        "category_name_english": "i5",
                        "category_name_arabic": "",
                        "category_date_edited": "2014-05-09 00:00:00",
                        "sub": []
                    },
                    {
                        "categoryID": "11",
                        "category_parentID": "6",
                        "category_name_english": "i7",
                        "category_name_arabic": "",
                        "category_date_edited": "2014-05-23 00:00:00",
                        "sub": []
                    }
                ]
            }
        ]
    },
    {
        "categoryID": "12",
        "category_parentID": "0",
        "category_name_english": "Mobile",
        "category_name_arabic": "",
        "category_date_edited": "2014-05-15 00:00:00",
        "sub": []
    }
]

我的代码在这里:

- (void)viewDidLoad
{
    [super viewDidLoad];
    RADataObject *category;
    for (NSDictionary *dict in JSONArray) {
        NSArray *subArray = [dict valueForKey:@"sub"];
        if(subArray.count ==0) {
            category = [RADataObject dataObjectWithName:[dict valueForKey:category_name_english] children:nil];
        }
        else {
            category = [RADataObject dataObjectWithName:[dict valueForKey:category_name_english] children:[self getSubCategories:subArray]];
        }
        [mutableAddArray addObject:category];
    }
    self.data = [mutableAddArray copy];
}

-(NSArray *)getSubCategories:(NSArray *)subArray
{
    NSMutableArray *duplicate = [[NSMutableArray alloc]init];
    for (NSDictionary *dct in subArray) {
        [duplicate addObject:[dct valueForKey:category_name_key]];
    }
    NSArray *returnArray = [duplicate copy];
    return returnArray;
}

是否可以使用递归?

3 个答案:

答案 0 :(得分:1)

你走了!运行它并希望它可以帮助你。

-(NSMutableArray *)parsingWithJsonArray:(NSArray *)array
{
   for (dictionary in array)
  {
    NSDictionary *categoryEnglishDict = [dictionary objectForKey:@"category_name_english"];

    [mutableArray addObject:categoryEnglishDict];

    NSArray *subArray = [dictionary objectForKey:@"sub"];

    [self parsingWithJsonArray:subArray];
  }

  return mutableArray;  //This mutable array would contain category english language.
}

其他任何事都让我知道。

答案 1 :(得分:1)

此代码可能适用于您的..未选中..

-(void)getSubCategories:(NSArray *)subArray
{
for (NSDictionary *myDict in subArray)
{

NSString *first = [myDict objectForKey : @"category_name_english"];
NSArray *myubArray = [myDict objectForKey : @"sub"];
if([mysubArray count])
 [self getSubCategories:mySubArray];


}
}

答案 2 :(得分:0)

首先,您应该定义一个数据存储对象来存储您的类别 -

SOCategory.h

@interface SOCategory : NSObject

-(NSArray *)subCategories;
+(SOCategory *)categoryWithDictionary:(NSDictionary *)dictionary;

@property (copy,nonatomic) NSString *categoryName;

@end

SOCategory.m

#import "SOCategory.h"

@interface SOCategory ()

@property (strong,nonatomic) NSMutableArray *subCategoryStore;

@end

@implementation SOCategory

-(id) init {
    self=[super init];
    if (self != nil) {
        _subCategoryStore=[[NSMutableArray alloc]init];
    }

    return self;
}

+(SOCategory *)categoryWithDictionary:(NSDictionary *)dictionary
{
    SOCategory *newCat=[[SOCategory alloc]init];
    if (newCat != nil) {
        newCat.categoryName=(NSString *)[dictionary objectForKey:@"category_name_english"];
        NSArray *subCats=(NSArray *)[dictionary objectForKey:@"sub"];
        for (NSDictionary *dict in subCats) {
            [newCat.subCategoryStore addObject:[SOCategory categoryWithDictionary:dict]];
        }
    }
    return newCat;
}

-(NSArray *)subCategories {
    return ([NSArray arrayWithArray:self.subCategoryStore]);
}

@end

然后,在现有代码中,您可以填充一系列类别 -

NSMutableArray *categories=[[NSMutableArray alloc]init];

for (NSDictionary *dict in JSONArray) {
   [categories addObject:[SOCategory categoryWithDictionary:dict]];
}

请注意,此代码不会报告格式错误的词典的错误。