我已经通读了与TouchJSON序列化相关的问题和答案,但我仍然没有得到它的工作。从NSDictionary TOUCHJSON序列化
我使用示例数据创建NSDictionary,并使用JSONTouch序列化程序将NSDictionary转换为JSON。然而,当我登录了NSData对象 'theJSONData',它给了我这样的结果:
< 7b223131 31353535 34343434 223a2250 ... 65227d>
此外,当我把这个“theJSONData “数据网络服务(即公司预计,JSON)这是我得到的结果:
2011-07-31 18:48:46.572路灯[7169:207]序列化错误:(空)
2011 -07-31 18:48:46.804路灯[7169:207] returnData:(null)
2011-07-31 18:48:46.805路灯[7169:207]错误:错误域= kJSONScannerErrorDomain代码= -201 “无法扫描数组。数组不是由'['字符开始的。“ UserInfo = 0x4d51ab0 {snippet =!HERE> !? xml version =”1.0“,location = 0,NSLocalizedDescription =无法扫描数组。 。,character = 0,line = 0}
我在做什么错误?JSON NSData对象'theJSONData'是否需要在将其发送到Web服务之前转换为另一种类型?我失踪?
// Create the dictionary
NSDictionary *outage = [[NSDictionary alloc] initWithObjectsAndKeys:
@"YCoord", @"12678967.543233",
@"XCoord", @"12678967.543233",
@"StreetLightID", @"666",
@"StreetLightCondition", @"Let's just say 'BAD'",
@"PhoneNumber", @"1115554444",
@"LastName", @"Smith",
@"Image",@"",
@"FirstName", @"Dawn",
@"Comments", @"Pole knocked down",
nil];
NSError *error = NULL;
// Serialize the data
NSData *theJSONData = [[CJSONSerializer serializer] serializeDictionary:outage error:&error];
NSLog(@"theJSONData: %@", theJSONData);
NSLog(@"Serialization Error: %@", error);
// Set up the request and send it
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://24.52.35.127:81/StreetLight/StreetlightService/CreateStreetLightOutage"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: theJSONData];
// Deserialize the response
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error:&error];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSData *theReturnData = [returnString dataUsingEncoding:NSUTF8StringEncoding];
id theObject = [[CJSONDeserializer deserializer] deserializeAsArray:theReturnData error:&error];
NSLog(@"returnData: %@",theObject);
NSLog(@"Error: %@", error);
两个快速件事:1)你可能要扭转你的NSDictionary DEFS(它的ObjectsAndKeys,不KeysAndObjects),2)使用一个程序,如HTTP客户看到你的服务器返回。这听起来像你的服务器不喜欢你发送的东西,并没有返回任何东西,这会给你错误。只是一个猜测。 – Colin