Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Serialization NSDictionary中的TOUCHJSON序列化_Serialization_Touchjson - Fatal编程技术网

Serialization NSDictionary中的TOUCHJSON序列化

Serialization NSDictionary中的TOUCHJSON序列化,serialization,touchjson,Serialization,Touchjson,我已经通读了与TouchJSON序列化相关的问题和答案,但我仍然没有让它发挥作用 我使用示例数据创建了一个NSDictionary,并使用JSONTouch序列化程序将NSDictionary转换为JSON。但是,当我记录NSData对象“theJSONData”时,结果是: // Create the dictionary NSDictionary *outage = [[NSDictionary alloc] initWithObjectsAndKeys:

我已经通读了与TouchJSON序列化相关的问题和答案,但我仍然没有让它发挥作用

我使用示例数据创建了一个NSDictionary,并使用JSONTouch序列化程序将NSDictionary转换为JSON。但是,当我记录NSData对象“theJSONData”时,结果是:

// 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);
此外,当我将此“theJSONData”数据发送到web服务(需要JSON)时,我得到的是:

2011-07-31 18:48:46.572路灯[7169:207]序列化错误:(null)

2011-07-31 18:48:46.804路灯[7169:207]返回数据:(空)

2011-07-31 18:48:46.805街灯[7169:207]错误:错误域=KJSONSCANERRORDOMAIN代码=-201“无法扫描数组。数组不是由“[”字符启动的。”UserInfo=0x4d51ab0{snippet=!HERE>!?xml version=“1.0”,location=0,NSLocalizedDescription=无法扫描数组。数组不是由['字符,字符=0,行=0}

我做错了什么?在我将JSON NSData对象“theJSONData”发送到web服务之前,是否需要将其转换为另一种类型?是否还缺少另一个步骤?


首先,您在NSDictionary中已将对象和键反转


我对TouchJSON了解不够,无法帮助您完成这部分代码。

首先,您的对象和键在NSDictionary中被颠倒了


我对TouchJSON了解不够,无法帮助处理这部分代码。

在解析goodle v3 api响应时,我遇到了类似的问题。仍然无法解决我的问题,但我发现可能对您有帮助的一点是,如果您使用反序列化数组,那么JSON响应必须包含在“[”和“]”中如果您是反序列化AsDictionary,那么JSON响应必须包含在“{”和“}”中

由于GoogleV3API JSON响应是“{”“}”格式的,所以我需要使用反序列化AsDictionary方法

我想你已经知道这一点了,但是在看了Jonathan Wight的代码之后,我已经解决了我自己的问题,因为Jonathan的代码在解析JSON响应时具体检查了上述内容

谢谢


Tim

我在解析goodle v3 api响应时遇到了类似的问题。仍然无法解决我的问题,但我发现一件可能对您有帮助的事情是,如果您正在使用反序列化数组,那么JSON响应必须包含在“[”和“]”中如果您是反序列化AsDictionary,那么JSON响应必须包含在“{”和“}”中

由于GoogleV3API JSON响应是“{”“}”格式的,所以我需要使用反序列化AsDictionary方法

我想你已经知道这一点了,但是在看了Jonathan Wight的代码之后,我已经解决了我自己的问题,因为Jonathan的代码在解析JSON响应时具体检查了上述内容

谢谢


Tim

感谢大家的帮助。我最终使用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);

感谢大家的帮助。我最终使用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)您可能想要反转nsdictionary def(它是ObjectsAndKeys,而不是KeysAndObjects),和2)使用Http Client之类的程序查看服务器返回的内容。听起来服务器不喜欢您发送的内容,并且什么也不返回,这会给您带来错误。只是猜测一下。两件简单的事情:1)您可能想要反转nsdictionary defs(它是ObjectsAndKeys,而不是KeysAndObjects),和2)使用Http Client之类的程序查看服务器返回的内容。听起来服务器不喜欢您发送的内容,并且什么也不返回,这会给您带来错误。只是猜测一下。嗨,我的问题是,在解析数据之前,我不允许从internet完全下载数据。我只是忘记了同步vs as同步NSURL问题。对red herring.yep表示歉意,通过使用异步进行修复,它的解析很好。以下是我用来修复问题的信息链接:嗨,我的问题是,我不允许在解析数据之前从internet完全下载数据。我只是忘记了同步与异步NSURL问题。为red herring道歉。是的,通过使用异步进行修复,它的解析很好。以下是我用来修复问题的信息链接:谢谢大家-我解决了我的问题。我使用Fiddler跟踪需要发送的内容,我意识到我没有正确格式化标题。以下是最终对我起作用的代码:谢谢大家-我知道了我解决了我的问题。我使用Fiddler跟踪需要发送的内容,我意识到我没有正确格式化标题。以下是最终对我有效的代码: