ios - 将对象转换为 json

标签 ios json nsmutabledictionary

<分区>

我有一个名为“Store”的主要实体类,例如:

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

商店.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

登录.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

现在我需要在服务器上发布这个 Store 对象。所以我使用下面的代码创建字典:

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"
            }
        }
    ]
}

您能否检查我的代码并让我知道我需要在这里进行哪些更改才能获得上述输出?

谢谢

最佳答案

然后你需要再创建一本字典

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"];

关于ios - 将对象转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562980/

上一篇:ios - 在IOS中,在 View Controller 中添加搜索显示 Controller 的位置

下一篇:ios - 在 hh :mm:ss format to NSDate for Timer Countdown 中转换 NSString

相关文章:

iphone - 在界面生成器中从 nib 文件加载 subview

json - 部署模板解析失败 : 'Required property ' type' not found in JSON. 路径 '',行,位置。'。 (代码:无效模板)

php - 返回带有 json 的 blob

ios - 如何删除NSUserDefaults内部的NSMutableDictionary内部的对象

ios - 用 for 循环填充 NSMutableDictionary,然后它会显示最后加载的值

ios - 填充 NSArray Json 数据

iphone - 将 GLInt 作为引用传递时遇到问题

ios - cocoa touch 框架

ios - Xcode 预处理器宏检查 Base SDK 是否 >= iOS 7.0

添加为另一个对象的属性时,JavaScript 表单序列化不正确