所以我有一个函数可以创建这样的字典并返回该字典 -
- (NSDictionary *) createPostData{
NSMutableDictionary * requestDictionary = [[NSMutableDictionary alloc]init];
[requestDictionary setValue:@"type://someStuff" forKey: @"$type"];
[requestDictionary setValue:@"account" forKey:@"Widget"];
NSMutableDictionary* messageDictionary = [[NSMutableDictionary alloc] init];
[messageDictionary setValue:@"type://someStuff" forKey:@"$type"];
[messageDictionary setValue: self.password forKey:@"Password"];
[messageDictionary setValue:false forKey:@"RememberMe"];
[requestDictionary setValue:messageDictionary forKey:@"Message"];
return requestDictionary;
}
然后我这样做
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDataObject
options:0 // Pass 0 if you don't care about the readability of the generated string
error: &error];
我得到了这个错误 -
Error - *** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write
我无法理解为什么,因为我认为我正在订阅文档中的规则 - 每个键都是一个字符串,而我只使用NSDictionary和NSString的实例。
发生了什么事?
答案 0 :(得分:2)
设置代码块
if ([NSJSONSerialization isValidJSONObject:postDataObject]) { NSData *postData = [NSJSONSerialization dataWithJSONObject:postDataObject
options:0 // Pass 0 if you don't care about the readability of the generated string
error: &error];}
而不是
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDataObject
options:0 // Pass 0 if you don't care about the readability of the generated string
error: &error];