【问题标题】:Json serialize array in objective-cJson在objective-c中序列化数组
【发布时间】:2012-11-11 07:36:26
【问题描述】:

我想将数组序列化为 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;

}

请帮帮我 每一个小提示都会非常感激。 谢谢!

【问题讨论】:

    标签: iphone objective-c xcode json cocoa


    【解决方案1】:

    为了在 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
    

    【讨论】:

      【解决方案2】:

      如果您的应用适用于 iOS 5 或更高版本,则可以使用 NSJSONSerialization:

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

      否则我推荐SBJson。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 2013-02-13
        • 2021-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多