0

配列を JSON にシリアライズしたい。これが私のJSONです-

{
  "User":{"Id":"1222","Email":"asdad@adasd.com"},
  "Person":{"Name":"John","Surname":"Smith"}
}

値 1222、asdad@adasd.com、John、Smith は例です。この値はコントローラーで定義されます。

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;

}

少し助けてください少しでもヒントをいただければ幸いです。ありがとう !

4

2 に答える 2

0

アプリが iOS 5 以降用の場合は、NSJSONSerialization を使用できます。

http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

それ以外の場合は、SBJson をお勧めします。

于 2012-11-23T00:09:50.980 に答える
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
于 2012-11-22T22:08:59.750 に答える