iOS:一般从 NSObject 类序列化/反序列化复杂的 JSON

标签 ios objective-c json generics jsonkit

有人知道如何序列化基于 NSObject 类的嵌套 JSON 吗?有一个序列化简单 JSON 的讨论 here ,但它不够通用,无法满足复杂的嵌套 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。有人对此有更好的解决方案吗? This library对于 C#,基于对象类满足序列化/反序列化。我想基于 NSObject 重现相同的东西

最佳答案

最后我们可以使用 JSONModel 轻松解决这个问题.这是迄今为止最好的方法。 JSONModel 是一个库,它通常基于类序列化/反序列化您的对象。您甚至可以使用基于非 nsobject 的属性,例如 intshortfloat。它还可以满足嵌套的复杂 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 }
                  ]}

关于iOS:一般从 NSObject 类序列化/反序列化复杂的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14958883/

相关文章:

ios - 用于去电 iOS webrtc 的 VOIP 拨号器提示音

ios - 不支持的 Swift 版本

objective-c - 我应该在返回 NSString 的方法中返回 NSMutableString

json - 将 Swagger JSON 转换为 RAML/YAML

C# JSon 解析器错误

java - 非法状态异常 : This is not a JSON Array (Gson Java)

ios - CFRunLoopWakeUp 不起作用?

objective-c - 如何获取机器的序列号

objective-c - iPhone SDK如何从照片库中获取完整的图像?

ios - 当没有执行任何操作并且用户点击通知时如何处理交互式通知?