首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自NSDictionary的TOUCHJSON序列化

来自NSDictionary的TOUCHJSON序列化
EN

Stack Overflow用户
提问于 2011-08-01 02:10:24
回答 3查看 1.8K关注 0票数 1

我已经阅读了与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:(空)

2011-07-31 18:48:46.805街灯7169:207错误:错误Domain=kJSONScannerErrorDomain代码=-201“无法扫描数组。数组没有由'[‘字符启动。” UserInfo=0x4d51ab0 {snippet=!HERE>!xml version="1.0“、location=0、NSLocalizedDescription=Could not扫描数组。数组未由“[”字符.、character=0、line=0}启动。

我做错什么了?在我将JSON NSData对象'theJSONData‘发送到web服务之前,是否需要将它转换为另一个类型?我又错过了一步吗?

代码语言:javascript
复制
// 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);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-06 21:49:03

谢谢大家的帮助。最后,我使用Fiddler跟踪需要用JSON发送到服务的内容,然后发现我没有正确地格式化标头。这是最后为我工作的代码。

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2011-08-01 02:26:05

首先,您已经在NSDictionary中反转了对象和键。

我对TouchJSON的了解还不足以帮助完成这部分代码。

票数 0
EN

Stack Overflow用户

发布于 2011-08-03 01:51:03

在解析一个很好的v3 api响应时,我遇到了类似的问题。离解决我的问题还差得很远,但我发现,如果您使用的是deserializerAsArray,那么JSON响应必须包含在"“和”deserializerAsDictionary“中,如果您是deserializerAsDictionary,那么JSON响应必须包含在"{”和"}“中。

由于google响应采用"{“}”格式,所以我需要使用v3方法。

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

谢谢,

时间

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6893665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档