2011-08-01 26 views
1

我已经通读了与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); 
+0

两个快速件事:1)你可能要扭转你的NSDictionary DEFS(它的ObjectsAndKeys,不KeysAndObjects),2)使用一个程序,如HTTP客户看到你的服务器返回。这听起来像你的服务器不喜欢你发送的东西,并没有返回任何东西,这会给你错误。只是一个猜测。 – Colin

回答

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); 
0

一件事,你有你的对象,并在NSDictionary的逆转键。

我不太了解TouchJSON以帮助完成这部分代码。

+0

谢谢,我发现了我的问题。我使用Fiddler来追踪需要发送的内容,并且我意识到我没有正确格式化标题。以下是最终为我工作的代码: –

0

我在解析goodle v3 api响应时遇到类似问题。仍然没有接近解决我的问题,但有一件事我发现可能对你有帮助的是,如果你使用反序列化器数组,那么JSON响应必须放在“[”和“]”中,如果你是deserializerAsDictionary,那么JSON回复必须包含在“{”和“}”中。

由于google v3 api JSON响应格式为“{”“}”,因此我需要使用deserialiserAsDictionary方法。

我怀疑你已经知道这一点,但在看了Jonathan Wight的代码之后,就像我已经解决了我自己的问题一样,因为Jonathan的代码在解析JSON响应时专门检查了上述内容。

感谢,

+0

嗨,我的问题是,在解析数据之前,我没有允许数据从互联网上完全下载。我简单地忘记了同步与异步NSURL问题。为红鲱鱼道歉。 – timv

+0

是的,通过使用异步修复,它解析得很好。下面是我用来解决我的问题的信息的链接:http://www.cocoabyss.com/foundation/nsurlconnection-synchronous-asynchronous/ – timv