2012-10-09 75 views
0

我對Mac開發非常陌生,並且遇到了一些麻煩,尋找好的資源。我目前的問題是一個自定義的Objective-C類對象序列化爲JSON。將OSX的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,請給我一個提示!任何類型的相關信息都會有幫助!謝謝!

+0

你怎麼想序列化的圖像?你想在JSON中嵌入圖像數據,可能是PNG或JPEG格式? –

+0

哦...對不起...我將它改爲只是簡單的NSString * imageName –

回答

2

如果你只是要序列包含整數和字符串對象,最簡單的方法是創建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]; 
} 
+0

謝謝,羅!我會試試這個! –

0

我一直在尋找這個過去周。我決定寫我自己的解決方案。它非常簡單,並建立在現有的Apple功能上。

在這裏看到:https://github.com/gslinker/GSObject

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

爲您的數據模型對象已經從GSObject而不是NSObject的繼承。這裏是ThingOne與來自GSObject繼承的例子:

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" 
    } 
} 
相關問題