2012-11-22 5 views
0

배열을 JSON으로 직렬화하려고합니다.Json은 objective-c로 배열을 serialize합니다.

{ 
    "User":{"Id":"1222","Email":"[email protected]"}, 
    "Person":{"Name":"John","Surname":"Smith"} 
} 

값 1222, [email protected], 존 스미스는 예 - 여기 내 JSON이다. 이 값은 컨트롤러에서 정의됩니다.

JSON에서이 배열을 직렬화 처리하는 방법을 모르겠습니다. 사용자에 따라 사용자와 사람을 다른 값으로 서버에 보내야합니다. 이것은 내 모델입니다. 둘 다 배열입니다.

다음은 나에게 모든 약간의 힌트가 진짜로 감사합니다 조금 도움,

헤더

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


@interface SaveUserProfileRequest : BaseRequest { 

NSMutableArray *_user; 
NSMutableArray *_person; 
NSMutableArray *_address; 
NSString *_userId; 
NSString *_userEmail; 
NSString *_userName; 
NSString *_userSurname; 


} 

- (id)initWithUser:(NSMutableArray*)user andPerson:(NSMutableArray*)person andAddress:(NSMutableArray*)address andUserId:(NSString*)userId andUserEmail:(NSString*)userEmail andUserName:(NSString*)userName andUserSurname:(NSString*)userSurname; 

@property (nonatomic, strong) NSMutableArray* user; 
@property (nonatomic, strong) NSMutableArray* person; 
@property (nonatomic, strong) NSMutableArray* address; 
@property (nonatomic, strong) NSString* userId; 
@property (nonatomic, strong) NSString* userEmail; 
@property (nonatomic, strong) NSString* userName; 
@property (nonatomic, strong) NSString* userSurname; 

@end 

구현

#import "SaveUserProfileRequest.h" 
#import "SaveUserProfileResponse.h" 
#import "OrderedDictionary.h" 
#import "BaseData.h" 

@implementation SaveUserProfileRequest 

@synthesize user=_user, person=_person, address=_address, userId=_userId, userEmail=_userEmail, userName=_userName, userSurname=_userSurname; 



- (id)initWithUser:(NSMutableArray*)user andPerson:(NSMutableArray*)person andAddress:(NSMutableArray*)address andUserId:(NSString*)userId andUserEmail:(NSString*)userEmail andUserName:(NSString*)userName andUserSurname:(NSString*)userSurname; { 
self = [super init]; 
if(self){ 
    self.user = user; 
    self.person = person; 
    self.address = address; 
    self.userId = userId; 
    self.userEmail = userEmail; 
    self.userName = userName; 
    self.userSurname = userSurname; 

} 
return self; 
} 


- (NSDictionary*) serialize{ 
OrderedDictionary *sup = [OrderedDictionary dictionaryWithCapacity:3]; 
[sup setValue:self.user forKey:@"User"]; 
[sup setValue:self.person forKey:@"Person"]; 

NSArray *users = [OrderedDictionary valueForKey:@"User"]; 
_user = [NSMutableArray arrayWithCapacity:[users count]]; 

for(NSDictionary *user in users){ 
    OrderedDictionary *userDict = [OrderedDictionary dictionaryWithCapacity:2]; 
    [userDict setValue:self.userId forKey:@"Id"]; 
    [userDict setValue:self.userEmail forKey:@"Mail"]; 
    return userDict; 
} 

NSArray *persons = [OrderedDictionary valueForKey:@"Person"]; 
_person = [NSMutableArray arrayWithCapacity:[persons count]]; 

for(NSDictionary *person in persons){ 
    OrderedDictionary *personsDict = [OrderedDictionary dictionaryWithCapacity:2]; 
    [personsDict setValue:self.userName forKey:@"Name"]; 
    [personsDict setValue:self.userSurname forKey:@"Surname"]; 
    return personsDict; 
} 
return sup; 

} 

제발 내 코드입니다. 감사합니다.

답변

0

Objective-C에서 JSON을 푸는 데는 항상 JSONKit을 사용합니다. 당신은 여기를 포크 할 수 있습니다 그것을 사용할 수있는 방법에 https://github.com/johnezang/JSONKit

몇 가지 예 :

NSArray *array = @[..]; 
NSString *arrayAsJsonString = [array JSONString]; 

NSString *string = @".."; 
id objectFromJsonString = [string objectFromJSONString]; // i.e an NSArray or NSDictionary