我想创建一个 json 字符串发送到服务器,但我不知道该怎么做。我正在使用以下代码,但它给了我一个例外。
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON 写入中的类型无效
NSError *error = nil;
self.drill.drillTitle = drillTitle.text;
self.drill.crewName = teamName.text;
self.drill.objectives = objectives.text;
self.drill.category = category;
self.drill.benchmarks = benchmarkArray;
self.drill.crewMembers = crewArray;
self.drill.currentUser = [Session retreiveSession];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.drill.drillTitle, @"drillTitle", self.drill.crewName, @"crewName", self.drill.objectives, @"objectives", self.drill.benchmarks, @"benchmarks", self.drill.crewMembers, @"crewMembers", self.drill.currentUser, @"currentUser", nil, self.drill.category, @"category", nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON output: %@", jsonString);
我的 Drill 课程在这里:
#import <Foundation/Foundation.h>
#import "Category.h"
#import "Users.h"
#import "Benchmark.h"
@interface Drill : NSObject
@property (nonatomic, retain) NSString *drillId;
@property (nonatomic, retain) NSString *drillTitle;
@property (nonatomic, retain) NSString *crewName;
@property (nonatomic, retain) NSString *objectives;
@property (nonatomic, retain) Category *category;
@property (nonatomic, retain) NSMutableArray *benchmarks;
@property (nonatomic, retain) NSMutableArray *crewMembers;
@property (nonatomic, retain) Users *currentUser;
@property (nonatomic, retain) NSString *totalDrillTime;
@property (nonatomic, retain) NSString *timestamp;
@property (nonatomic, retain) NSString *error;
- (id)initWithCoder:(NSCoder *)coder;
- (void)encodeWithCoder:(NSCoder *)encoder;
@end
#import "Drill.h"
@implementation Drill
@synthesize drillId;
@synthesize drillTitle;
@synthesize crewName;
@synthesize objectives;
@synthesize category;
@synthesize benchmarks;
@synthesize crewMembers;
@synthesize currentUser;
@synthesize totalDrillTime;
@synthesize timestamp;
@synthesize error;
- (id)initWithCoder:(NSCoder *)coder {
self = [super init];
if(self) {
drillId = [coder decodeObjectForKey:@"id"];
drillTitle = [coder decodeObjectForKey:@"drillTitle"];
crewName = [coder decodeObjectForKey:@"crewName"];
objectives = [coder decodeObjectForKey:@"objectives"];
category = [coder decodeObjectForKey:@"category"];
benchmarks = [coder decodeObjectForKey:@"benchmarks"];
crewMembers = [coder decodeObjectForKey:@"crewMembers"];
currentUser = [coder decodeObjectForKey:@"currentUser"];
totalDrillTime = [coder decodeObjectForKey:@"totalDrillTime"];
timestamp = [coder decodeObjectForKey:@"timestamp"];
error = [coder decodeObjectForKey:@"error"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:drillId forKey:@"id"];
[encoder encodeObject:drillTitle forKey:@"drillTitle"];
[encoder encodeObject:crewName forKey:@"crewName"];
[encoder encodeObject:objectives forKey:@"objectives"];
[encoder encodeObject:category forKey:@"category"];
[encoder encodeObject:benchmarks forKey:@"benchmarks"];
[encoder encodeObject:crewMembers forKey:@"crewMembers"];
[encoder encodeObject:currentUser forKey:@"currentUser"];
[encoder encodeObject:totalDrillTime forKey:@"totalDrillTime"];
[encoder encodeObject:timestamp forKey:@"timestamp"];
[encoder encodeObject:error forKey:@"error"];
}
@end