*** Terminating app due to uncaught exception 'NSinvalidargumentexception',reason: '*** +[NSJsONSerialization writeJsONObject:toStream:options:error:]: InvalID top-level type in JsON write'
这涉及到这一块代码(基本上我正在创建一些JsON并将其发送到服务器).我已经检查过服务器以查看套接字是否已打开,它是什么!
NSDictionary *blobData = [NSDictionary dictionaryWithObjectsAndKeys: sendString,@"string",nil];Nsstring *blobString = [[Nsstring alloc] initWithData:[NSJsONSerialization dataWithJsONObject:blobData options:kNilOptions error:&error] enCoding:NSUTF8StringEnCoding];NSLog(@"Blob Created: %@",blobString);NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys: @"send_string",@"request_type",[NSNumber numberWithInt:0],@"security_level",@"ios",@"device_type",//No Email ProvIDed,This is just for testing blobData,@"blob",nil];NSData *JsONRequestData = NulL;if ([NSJsONSerialization isValIDJsONObject:requestData]) { NSLog(@"Proper JsON Object"); JsONRequestData = [NSJsONSerialization dataWithJsONObject:requestData options:kNilOptions error:&error];}else { NSLog(@"requestData was not a proper JsON object"); return FALSE;}NSLog(@"Error:%@",[[error userInfo] objectForKey:@"NSDeBUGDescription"]);NSLog(@"Contents of JsONRequestData: %@",[[Nsstring alloc] initWithData:JsONRequestData enCoding:NSUTF8StringEnCoding]);[NSJsONSerialization writeJsONObject:JsONRequestData toStream:outputStream options:0 error:&error];
我是否错误地创建了JsON对象?也许我处理“blob”的方式有问题
这是我创建JsONRequestData后NSLogs打印的内容
{"security_level":"0","request_type":"send_string","device_type":"ios","blob":{"string":"hello"}}解决方法 writeJsONObject期望您要发送的实际反序列化对象,因此在这种情况下,您需要将requestData对象传递给它,而不是像JsONRequestData那样:
NSJsONSerialization writeJsONObject:requestData toStream:outputStream options:0 error:&error];总结
以上是内存溢出为你收集整理的iOS中的JSON错误:JSON写入中的顶级类型无效全部内容,希望文章能够帮你解决iOS中的JSON错误:JSON写入中的顶级类型无效所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)