2013-12-13 114 views
0

我有一個主要的實體類名稱爲 「商店」,如:轉換對象到JSON

Store.h: -

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

@interface Store : NSObject 

@property (nonatomic, retain) NSString *storeId; 
@property (nonatomic, retain) NSString *storeProfileId; 
@property (nonatomic, retain) NSString *storeName; 
@property (nonatomic, retain) NSString *storeRegion; 

@property (nonatomic, retain) SignIn *signIn; 

@end 

Store.m: -

#import "Store.h" 

@implementation Store 

@synthesize storeId, storeProfileId, storeName, storeRegion, signIn; 

- (id) initWithCoder: (NSCoder *)coder 
{ 
    self = [[Store alloc] init]; 
    if (self != nil) 
    { 
     self.storeId = [coder decodeObjectForKey:@"storeId"]; 
     self.storeProfileId = [coder decodeObjectForKey:@"storeProfileId"]; 
     self.storeName = [coder decodeObjectForKey:@"storeName"]; 
     self.storeRegion = [coder decodeObjectForKey:@"storeRegion"]; 

     self.signIn = [coder decodeObjectForKey:@"signIn"]; 
    } 
    return self; 
} 

- (void)encodeWithCoder: (NSCoder *)coder 
{ 
    [coder encodeObject:storeId forKey:@"storeId"]; 
    [coder encodeObject:storeProfileId forKey:@"storeProfileId"]; 
    [coder encodeObject:storeName forKey:@"storeName"]; 
    [coder encodeObject:storeRegion forKey:@"storeRegion"]; 

    [coder encodeObject:signIn forKey:@"signIn"]; 
} 

@end 

這裏在Store類中,我正在使用另一個類名「Sign In」,其中包含一些其他屬性。

SignIn.h: -

#import <Foundation/Foundation.h> 

@interface SignIn : NSObject 

@property (nonatomic, retain) NSString *inTime; 
@property (nonatomic, retain) NSString *outTime; 
@property (nonatomic, retain) NSString *isStatus; 

@end 

SignIn.m: -

#import "SignIn.h" 

@implementation SignIn 
@synthesize inTime, outTime, isStatus; 

- (id) initWithCoder: (NSCoder *)coder 
{ 
    self = [[SignIn alloc] init]; 
    if (self != nil) 
    { 
     self.inTime = [coder decodeObjectForKey:@"inTime"]; 
     self.outTime = [coder decodeObjectForKey:@"outTime"]; 
     self.isStatus = [coder decodeObjectForKey:@"isStatus"]; 
    } 
    return self; 
} 

- (void)encodeWithCoder: (NSCoder *)coder 
{ 
    [coder encodeObject:inTime forKey:@"inTime"]; 
    [coder encodeObject:outTime forKey:@"outTime"]; 
    [coder encodeObject:isStatus forKey:@"isStatus"]; 
} 

@end 

現在我需要張貼在服務器此店鋪對象。所以我用下面的代碼創建詞典:

NSMutableArray *storeJSONArray=[NSMutableArray array]; 
    for (Store *store in array1) { 

     NSMutableDictionary *storeJSON=[NSMutableDictionary dictionary]; 

     [storeJSON setValue:store.storeId forKey:@"storeId"]; 
     [storeJSON setValue:store.storeProfileId forKey:@"storeProfileId"]; 
     [storeJSON setValue:store.storeName forKey:@"storeName"]; 
     [storeJSON setValue:store.storeRegion forKey:@"storeRegion"]; 


     //Sign In 
     [storeJSON setValue:store.signIn.inTime forKey:@"inTime"]; 
     [storeJSON setValue:store.signIn.outTime forKey:@"outTime"]; 
     [storeJSON setValue:store.signIn.isStatus forKey:@"isStatus"]; 

     [storeJSONArray addObject:storeJSON]; 
    } 

    NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary]; 
[dictionnary setObject:storeJSONArray forKey:@"Store"]; 

NSError *error = nil; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary 
                options:kNilOptions 
                error:&error]; 

if (! jsonData) { 
    NSLog(@"Got an error: %@", error); 
} else { 
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
} 

但作爲一個輸出,我得到jsonString值,如:

{ 
    "Store": [ 
     { 
      "storeId": "SGVM0001", 
      "storeProfileId": "SG-12", 
      "store_name": "Best Denki", 
      "storeRegion": "Ngee Ann City", 
      "inTime": "2013-12-05 11:03:00", 
      "outTime": "2013-12-0511: 27: 00", 
      "isStatus": "YES" 
     } 
    ] 
} 

但我需要的輸出爲:

{ 
    "Store": [ 
     { 
      "storeId": "SGVM0001", 
      "storeProfileId": "SG-12", 
      "store_name": "Best Denki", 
      "storeRegion": "Ngee Ann City", 
      "signIn": { 
       "inTime": "2013-12-05 11:03:00", 
       "outTime": "2013-12-0511: 27: 00", 
       "isStatus": "YES" 
      } 
     } 
    ] 
} 

燦你請檢查我的代碼,讓我知道我需要做什麼更改以獲得以上輸出?

感謝

回答

1

然後,你需要創建一個更詞典

NSDMutableDict *signInDic = [NSMutableDictionary dictionary]; 
//Sign In 
[signInDic setValue:store.signIn.inTime forKey:@"inTime"]; 
[signInDic setValue:store.signIn.outTime forKey:@"outTime"]; 
[signInDic setValue:store.signIn.isStatus forKey:@"isStatus"]; 

[storeJSON setObject:signInDic forKey:@"sign_in"]; 
+0

@BhumeshwerThanks給我時間。實際上,我在一個主要實體「Store」中有6個子實體類。所以就像你上面的代碼我需要爲所有人做。這意味着我需要爲子實體創建多個字典。 –

0
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 

    [dict setValue:store.dict.inTime forKey:@"inTime"]; 

     ....... 

     ..... 

    [storeJSON setObject:dict forKey:@"sign_in"];