首頁>Program>source

有人知道如何基於NSObject類序列化巢狀的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

如何基於類對它们进行序列化/反序列化?

EDIT

目前,我可以根据任何類生成這樣的有效負載:

NSMutableDictionary *Payload = [self serialize:objClass];

但是它不能满足巢狀的複雜JSON.有人對此有更好的解決方案吗?此庫用於C#,可以根据物件類进行序列化/反序列化.我想基於NSObject複製相同的东西

最新回復
  • 5月前
    1 #

    最後,我们可以使用JSONModel轻松解決此問题.這是迄今為止最好的方法. JSONModel是一个庫,该庫通常基於Class對物件进行序列化/反序列化.您甚至可以將基於非nsobject的屬性用於 intshortfloat .它還可以满足巢狀複雜的JSON。

    1) Deserialize example .通過參考上面的示例,在頭檔案中:

    #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) Serialize Example .在實現檔案中:

    #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 }
                      ]}
    

  • 5月前
    2 #

    您必须提前知道要反序列化哪種物件.在這種情况下,您將要反序列化為 NSDictionary 具有两个屬性:"会計"和"销售".這些屬性中的每一个都是 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];
    

    在此示例中,您應该可以推斷出更通用的解串器.也就是說,您希望遍歷資料以將"未知"通用物件建立為"已知"特定物件的深層副本。

  • 5月前
    3 #

    也许這可以帮助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];
    ...
    

  • 清除案例問题
  • c:fgets()在末尾包含換行符