我已經通讀了與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