10

任何人都知道如何基于 NSObject 类序列化嵌套的 JSON?这里有一个关于序列化简单 JSON 的讨论但它不够通用,无法满足复杂的嵌套 JSON。

想象一下这是 JSON 的结果:

{ "accounting" : [{ "firstName" : "John",  
                    "lastName"  : "Doe",
                    "age"       : 23 },

                  { "firstName" : "Mary",  
                    "lastName"  : "Smith",
                    "age"       : 32 }
                              ],                            
  "sales"      : [{ "firstName" : "Sally", 
                    "lastName"  : "Green",
                    "age"       : 27 },

                  { "firstName" : "Jim",   
                    "lastName"  : "Galley",
                    "age"       : 41 }
                  ]}

从这个类:

@interface Person : NSObject{}
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSNumber *age;
@end

@interface Department : NSObject{}
@property (nonatomic, strong) NSMutableArray *accounting; //contain Person class
@property (nonatomic, strong) NSMutableArray *sales; //contain Person class
@end

一般如何基于类对它们进行序列化/反序列化?

编辑

目前我能够基于任何类生成这样的有效负载:

NSMutableDictionary *Payload = [self serialize:objClass];

但它不适合嵌套的复杂 JSON。有人对此有更好的解决方案吗?这个C# 库基于对象类满足序列化/反序列化。我想基于 NSObject 重现相同的东西

4

3 回答 3

12

最后,我们可以使用JSONModel轻松解决这个问题。这是迄今为止最好的方法。JSONModel 是一个基于 Class 对对象进行一般序列化/反序列化的库。您甚至可以使用基于非 nsobject 的属性,例如int,shortfloat. 它还可以满足嵌套复杂的 JSON。

1)反序列化示例。参考上面的例子,在头文件中:

#import "JSONModel.h"

@interface Person : JSONModel 
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSNumber *age;
@end

@protocol Person;

@interface Department : JSONModel
@property (nonatomic, strong) NSMutableArray<Person> *accounting;
@property (nonatomic, strong) NSMutableArray<Person> *sales;
@end

在实现文件中:

#import "JSONModelLib.h"
#import "myJSONClass.h"

NSString *responseJSON = /*from example*/;
Department *department = [[Department alloc] initWithString:responseJSON error:&err];
if (!err)
{
    for (Person *person in department.accounting) {

        NSLog(@"%@", person.firstName);
        NSLog(@"%@", person.lastName);
        NSLog(@"%@", person.age);         
    }

    for (Person *person in department.sales) {

        NSLog(@"%@", person.firstName);
        NSLog(@"%@", person.lastName);
        NSLog(@"%@", person.age);         
    }
}

2)序列化示例。在实现文件中:

#import "JSONModelLib.h"
#import "myJSONClass.h"

Department *department = [[Department alloc] init];

Person *personAcc1 = [[Person alloc] init];
personAcc1.firstName = @"Uee";
personAcc1.lastName = @"Bae";
personAcc1.age = [NSNumber numberWithInt:22];
[department.accounting addOject:personAcc1];

Person *personSales1 = [[Person alloc] init];
personSales1.firstName = @"Sara";
personSales1.lastName = @"Jung";
personSales1.age = [NSNumber numberWithInt:20];
[department.sales addOject:personSales1];

NSLog(@"%@", [department toJSONString]);

这是序列化示例的 NSLog 结果:

{ "accounting" : [{ "firstName" : "Uee",  
                    "lastName"  : "Bae",
                    "age"       : 22 }
                 ],                            
  "sales"      : [{ "firstName" : "Sara", 
                    "lastName"  : "Jung",
                    "age"       : 20 }
                  ]}
于 2013-05-27T10:53:35.550 回答
1

您必须提前知道要反序列化的对象类型。在这种情况下,您将反序列化为NSDictionary具有两个属性:“accounting”和“sales”。这些属性中的每一个都是NSArray. 数组将有NSDictionary.

既然您知道这些对象中的每一个究竟是什么一旦您将 JSON 反序列化为本机对象,您就可以从反序列化的对象中创建类的新实例。例如:

JSONDecoder decoder = [[JSONDecoder alloc] init];
NSObject notJSON = [decoder objectWithData:jsonData];
// where jsonData is an NSData representation of your JSON
[decoder release];

Person person1 = (Person)[notJSON objectForKey:@"accounting"][0];

鉴于此示例,您应该能够推断出更通用的反序列化器。也就是说,您希望遍历数据以创建“未知”通用对象到“已知”特定对象的深层副本。

于 2013-02-19T13:47:17.247 回答
0

也许这个可以帮助BWJSONMatcher。它可以帮助您通过一行代码轻松地将 JSON 字符串或 JSON 对象与您的数据模型匹配起来。

...
NSString *jsonString = @"{your-json-string}";
YourValueObject *dataModel = [YourValueObject fromJSONString:jsonString];

NSDictionary *jsonObject = @{your-json-object};
YourValueObject *dataModel = [YourValueObject fromJSONObject:jsonObject];
...
YourValueObject *dataModel = instance-of-your-value-object;
NSString *jsonString = [dataModel toJSONString];
NSDictionary *jsonObject = [dataModel toJSONObject];
...
于 2015-10-29T03:50:34.567 回答