2011-08-01 25 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