我有一个任务要求我将字符串作为 json 对象,在发送对象之前,我必须将此 json 对象放入 http 标头中。这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *nid = @"";
NSString *vocab = @"";
NSString *inturl = @"testoverview";
NSString *mail = @"chh@fbr.dk";
NSString *md5pw = @"4d57e7ef1b7c3f431aca424764e9d786";
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
nid, @"nid",
vocab, @"vocab",
inturl, @"inturl",
mail, @"mail",
md5pw, @"md5pw",nil];
NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:chh@fbr.dk, md5pw:4d57e7ef1b7c3f431aca424764e9d786}";
NSError *error;
NSString *url = @"http://udv.taenk.dk/chh/drupal-taenk/services/mobile";
NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:30.0];
NSURLConnection *connction = [[NSURLConnection alloc] initWithRequest:request delegate:self];
FSRemoteExecutor *remote = [[FSRemoteExecutor alloc] init];
[remote execute:url andHandler:[FSProductTestHandler alloc] JSONString:jsonString JSONData:jsonData Connection:connction];
[remote connection:connction didReceiveData:jsonData];
[remote connectionFinishedLoading:connction];
我的问题是我不能将 jsonDictionary 与对象一起使用并发送它,因为服务必须接收的字符串格式是这样的:
"{"nid":"","vocab":"", "inturl":"testoverview", "mail":"", "md5pw":""}"
字典会在字符串中插入 =,这不会给我服务的响应。
我想将字符串(代码中的 json)作为 dataWithJSONObject:jso 发送,如下所示:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];
但我收到一个错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* + [NSJSONSerialization dataWithJSONObject:options:error:]: JSON 写入中的顶级类型无效”
有人可以帮我吗?