我已经阅读了与TouchJSON序列化相关的问题和答案,但我仍然没有让它发挥作用。
我使用示例数据创建了一个NSDictionary,并使用JSONTouch序列化程序将NSDictionary转换为JSON。但是,当我记录NSData对象'theJSONData'时,它会给我这样的结果:
< 7b223131 31353535 34343434 223a2250 ... 65227d>
此外,当我将这个'theJSONData'数据发送到Web服务(期望JSON)时,这就是我得到的:
2011-07-31 18:48:46.572路灯[7169:207]序列化错误:(null)
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 =无法扫描数组。数组不是由'['字符。,字符= 0,行= 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);
答案 0 :(得分:1)
感谢大家的帮助。我最终使用Fiddler跟踪需要在JSON中发送到服务的内容,然后看到我没有正确格式化标题。这是最终为我工作的代码。
// Create the NSDictionary
NSDictionary *outage = [[NSDictionary alloc] initWithObjectsAndKeys:
@"12.543233",@"YCoord",
@"12.543233",@"XCoord",
@"111",@"StreetLightID",
@"Dented pole",@"StreetLightCondition",
@"1115554444",@"PhoneNumber",
@"Black",@"LastName",
[NSNull null],@"Image",
@"White",@"FirstName",
@"Hit by a car",@"Comments",
nil];
// Serialize the data
NSError *error = NULL;
NSData *theJSONData = [[CJSONSerializer serializer] serializeDictionary:outage error:&error];
NSLog(@"Serialization Error: %@", error);
// Change the data back to a string
NSString* theStringObject = [[NSString alloc] initWithData:theJSONData encoding:NSUTF8StringEncoding];
// Determine the length of the data
NSData *requestData = [NSData dataWithBytes: [theStringObject UTF8String] length: [theStringObject length]];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [requestData length]];
// Create request to send to web service
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://11.22.33.444:55/StreetLight/StreetlightService/CreateStreetLightOutage"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request setTimeoutInterval:30.0];
// 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: %@",returnString);
NSLog(@"Error: %@", error);
答案 1 :(得分:0)
首先,你已经在NSDictionary中反转了你的对象和键。
我不太了解TouchJSON来帮助处理这部分代码。
答案 2 :(得分:0)
我在解析goodle v3 api响应方面遇到了类似的问题。仍然没有更接近解决我的问题,但我发现可能对你有帮助的一件事是,如果你使用deserializerAsArray然后JSON响应必须包含在“[”和“]”中,如果你是deserializerAsDictionary那么JSON响应必须包含在“{”和“}”中。
由于google v3 api JSON响应采用“{”“}”格式,我需要使用deserialiserAsDictionary方法。
我怀疑你已经知道了这一点,但是在查看Jonathan Wight的代码之后,就我解决自己的问题而言,这是因为Jonathan的代码在解析JSON响应时特定于检查上述内容。
谢谢,
添