2013-04-04 8 views
5

Dall'applicazione iPhone, devo inviare la data come parametro a un metodo webservice in cui la logica di analisi del server è implementata con C#, .NET e JSON.invia datetime dall'app iphone a un servizio web

Nella mia applicazione iPhone sto formattazione della data come:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; 
NSString *myDateString = [dateFormatter stringFromDate:SentOn]; 

sto ottenendo l'errore:

There was an error deserializing the object of type DashboardDataContract.Contracts.DashboardInputParams. DateTime content '2013-04-04T12:00:00Z' does not start with '/Date(' and end with ')/' as required for JSON.'.

ho provato la formattazione diverso di date. Qualcuno potrebbe aiutarmi in questo senso?

risposta

7

Per impostazione predefinita la libreria JSON.Net emetterà tutte le date utilizzando l'esempio formato di data ISO "2012-05-09T00:00:00Z", ma formattatore JSON di Microsoft per la data emetterà utilizzando proprio formato di Microsoft “/Date(xxxxxxxxxxxx)/”

Si può provare questo:

unsigned long long time=(([[NSDate date] timeIntervalSince1970])*1000); 
NSString* dateTimeTosend= [NSString stringWithFormat:@"/Date(%lld)/",time]; 
+0

Molto bello e semplice! – user2068793

+0

Funzionerà su tutti i fusi orari? – Anupdas

+0

Se si desidera aggiungere il fuso orario, è necessario il formato/Data (xxxxxxxxxxxx + aaaa)/dove yyyy è il fuso orario ad esempio:/Data (12345678954 + 0000) / –

0

Il modo più efficiente per inviare NSDate al server è ottenere il timestamp. Fondamentalmente si può ottenere con

NSTimeInterval timestamp = [[NSDate date] timeInvervalSince1970]; 
0

È possibile creare un metodo di categoria in NSDate

- (NSString *)jsonDateString 
{ 
    NSTimeInterval seconds = [self timeIntervalSince1970]; 
    return [NSString stringWithFormat:@"/Date(%.0f+0000)/",seconds*1000]; 

} 
Problemi correlati