将objective-c自定义对象序列化为OSX的JSON?

Posted

技术标签:

【中文标题】将objective-c自定义对象序列化为OSX的JSON?【英文标题】:serialize objective-c custom object to JSON for OSX? 【发布时间】:2012-10-09 21:37:33 【问题描述】:

我是 Mac 开发的新手,在寻找好的资源时遇到了一些麻烦。我当前的问题是自定义objective-c 类对象序列化为JSON。

我知道苹果库中有一个内置的序列化程序,但它只适用于 Foundation 对象。

就我而言,我有自己的类,如下所示:

@interface SomeClass : NSObject

   int a;
   int b;
   NSString *aa;
   NSString *bb;

@property int a,b;
@property NSString *aa,*bb;

@end

如果有人知道如何将这种类型的结构序列化为 JSON,请给我一个提示!任何相关信息都会有所帮助!谢谢!

【问题讨论】:

你想如何序列化图像?是否要将图像数据嵌入 JSON,可能是 PNG 或 JPEG 格式? 哦...对不起...我将其更改为简单的 NSString *imageName 【参考方案1】:

如果只想序列化包含整数和字符串的对象,最简单的方法是创建NSJSONSerialization 支持的数据结构并对其进行序列化:

static const NSString *kAKey = @"a";
static const NSString *kBKey = @"b";
static const NSString *kAaKey = @"aa";
static const NSString *kBbKey = @"bb";

- (id)JSONObject 
    return @
        kAKey: @(self.a),
        kBKey: @(self.b),
        kAaKey: self.aa,
        kBbKey: self.bb
    ;


- (NSData *)JSONData 
    return [NSJSONSerialization dataWithJSONObject:[self JSONObject] options:0 error:NULL];

【讨论】:

谢谢你,罗伯!我会试试这个!【参考方案2】:

过去一周我一直在研究这个问题。我决定编写自己的解决方案。它非常简单,并且建立在现有的 Apple 功能之上。

请看这里:https://github.com/gslinker/GSObject

在这里:http://digerati-illuminatus.blogspot.com/2016/01/objective-c-and-json-convert-subclass.html

对于您的数据模型对象,让它继承自 GSObject 而不是 NSObject。下面是一个继承自 GSObject 的 ThingOne 示例:

ThingOne* object1 = [[ThingOne alloc] init];
object1.name = @"John Jones";


NSData* jsonData1 = [object1 toJsonDataWithOptions:NSJSONWritingPrettyPrinted];
NSString *jsonString1 = [object1 toJsonStringWithOptions:NSJSONWritingPrettyPrinted];

NSDictionary<NSString *,id> *dict1 =  [GSObject dictionaryWithValues:object1];

NSString *roundTripJson1 = [object1 toJsonStringWithOptions:NSJSONWritingPrettyPrinted];




//
//  ThingOne.h
//  JasonStuff
//
//  Created by Geoffrey Slinker on 12/28/15.
//  Copyright © 2015 Slinkworks LLC. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GSObject.h"
#import "ThingTwo.h"

@interface ThingOne : GSObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) ThingTwo *thingTwo;
@property (nonatomic, retain) NSArray *values;
@property (nonatomic, retain) NSDictionary *dict;
@property int myInt;
@property float myFloat;
@property BOOL myBool;
@property (nonatomic, retain) NSNumber* someMoney;


@end

//
//  ThingOne.m
//  JasonStuff
//
//  Created by Geoffrey Slinker on 12/28/15.
//  Copyright © 2015 Slinkworks LLC. All rights reserved.
//

#import "ThingOne.h"

@implementation ThingOne

@synthesize name;
@synthesize thingTwo;
@synthesize values;
@synthesize dict;
@synthesize myInt;
@synthesize myFloat;
@synthesize myBool;
@synthesize someMoney;

- (instancetype)init

self = [super init];

thingTwo = [[ThingTwo alloc] init];

thingTwo.stuff = @"Thing Two Stuff";
thingTwo.someOtherStuff = @"Thing Two Other Stuff";
NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
[dateFormater setDateFormat:@"yyyy-mm-dd"];
thingTwo.someDate =  [dateFormater dateFromString:@"1963-10-07"];

values = [NSArray arrayWithObjects:@"Value1", @"Value2", @"Value3", nil];

dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];

myInt = 5431;
myFloat = 123.456f;
myBool = YES;

someMoney = [NSNumber numberWithInt:503];

return self;


@end

//
//  ThingTwo.h
//  JasonStuff
//
//  Created by Geoffrey Slinker on 12/28/15.
//  Copyright © 2015 Slinkworks LLC. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GSObject.h"

@interface ThingTwo : GSObject

@property (nonatomic, retain) NSString *stuff;
@property (nonatomic, retain) NSString *someOtherStuff;
@property (nonatomic, retain) NSDate *someDate;
@property (nonatomic, retain) NSString *nullString;
@property (nonatomic, retain) NSDate *nullDate;

@end

//
//  ThingTwo.m
//  JasonStuff
//
//  Created by Geoffrey Slinker on 12/28/15.
//  Copyright © 2015 Slinkworks LLC. All rights reserved.
//

#import "ThingTwo.h"

@implementation ThingTwo

@synthesize stuff;
@synthesize someOtherStuff;
@synthesize someDate;

- (instancetype)init

self = [super init];

someDate = [NSDate date];

return self;


@end

以下是 JSON 输出的示例:


  "values" : [
    "Value1",
    "Value2",
    "Value3"
  ],
  "myInt" : 5431,
  "myFloat" : 123.456,
  "myBool" : true,
  "someMoney" : "$503.00",
  "thingTwo" : 
    "stuff" : "Thing Two Stuff",
    "nullDate" : null,
    "someDate" : "1963-01-07 07:10:00 +0000",
    "nullString" : null,
    "someOtherStuff" : "Thing Two Other Stuff"
  ,
  "name" : "John Jones",
  "dict" : 
    "key1" : "value1",
    "key2" : "value2"
  

【讨论】:

以上是关于将objective-c自定义对象序列化为OSX的JSON?的主要内容,如果未能解决你的问题,请参考以下文章

将填充了位和字节的 ushort[] 反序列化为自定义对象

使用 ObjectMapper 将 JSON 日期格式从 ZonedDateTime 序列化为自定义日期格式

ASP Web API:将对象序列化为 JSON 时指定自定义字段名称

如何将多个键值条目的 JSON 对象反序列化为 Rust 中的自定义结构

如何使用json.net自定义反序列化为对象

将对象序列化为 JSON