【发布时间】:2013-12-12 12:35:04
【问题描述】:
我有一个名为“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
Store.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 类中,我又取了一个类名“登录”,其中包括一些其他属性。
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:@"StoreRequest"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
options:kNilOptions
error:&error];
NSString *urlString =@"http://...................php";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
但是我没有得到正确的 JSON,请你检查我的代码,让我知道我的错误在哪里。提前致谢。
【问题讨论】:
-
不清楚你在问什么。
-
@Virussmcahere 是我的一个名为“Store”的实体类,只有我需要将该类对象转换为 JSON。但我不知道我在哪里弄错了。
-
不,实际上它并没有在任何地方崩溃,但它不是以 JSON 格式出现的,而且价值也没有出现。
-
你应该使用
NSJSONSerialization -
我的 JSON 在一个范围内。但它应该包含多个范围。
标签: ios objective-c json nsmutabledictionary