Ios 将对象转换为json

Ios 将对象转换为json,ios,json,nsmutabledictionary,Ios,Json,Nsmutabledictionary,我有一个名为“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

我有一个名为“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类中,我使用了另外一个类名“Sign-in”,其中包括一些其他属性

签名h:-

#import <Foundation/Foundation.h>

@interface SignIn : NSObject

@property (nonatomic, retain) NSString *inTime;
@property (nonatomic, retain) NSString *outTime;
@property (nonatomic, retain) NSString *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"
            }
        }
    ]
}
你能检查一下我的代码,让我知道我需要在这里做什么更改才能获得以上的输出吗


谢谢

那么你需要再创建一本字典

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

@谢谢你给我时间。实际上,我在一个主实体“存储”中有6个子实体类。所以就像你上面的代码一样,我需要为所有人做。这意味着我需要为子实体创建多个字典。
{
    "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"];
    NSMutableDictionary  *dict = [NSMutableDictionary dictionary];

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

      .......

      .....

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