2012-11-21 9 views
10

Voglio conservare la seguente stringa in una stringa variabileCome scrivere il valore della stringa JSON nel codice?

{ "id": "123", "DateOfRegistration": "2012-10-21T00: 00: 00 + 05: 30", "Stato ": 0}

questo è il codice che uso ..

String str="{"Id":"123","DateOfRegistration":"2012-10-21T00:00:00+05:30","Status":0}"; 

.. ma sta mostrando errore ..

+2

Qui è abbastanza ovvio, ma per la domanda successiva si dovrebbe dettaglio cosa errore sta mostrando. È il tipo di informazioni preziose che aiutano coloro che vogliono aiutarti. – Guillaume

+0

È necessario selezionare una risposta e/o una risposta positiva che è stata utile. – William

risposta

21

Devi fare questo

String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}"; 


prega see this for reference
anche from msdn :)

Short Notation UTF-16 character Description 
\' \u0027 allow to enter a ' in a character literal, e.g. '\'' 
\" \u0022 allow to enter a " in a string literal, e.g. "this is the double quote (\") character" 
\\ \u005c allow to enter a \ character in a character or string literal, e.g. '\\' or "this is the backslash (\\) character" 
\0 \u0000 allow to enter the character with code 0 
\a \u0007 alarm (usually the HW beep) 
\b \u0008 back-space 
\f \u000c form-feed (next page) 
\n \u000a line-feed (next line) 
\r \u000d carriage-return (move to the beginning of the line) 
\t \u0009 (horizontal-) tab 
\v \u000b vertical-tab 
+0

non dovresti sfuggire alla citazione finale;) – Brian

+0

hahahaha esattamente .. :) – Freak

2

dovete fuggire le virgolette all'interno della stringa in questo modo:

String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}"; 
+0

Infatti, @MikeBarnes, ma se osservi attentamente il timestamp vedrai che la mia risposta è stata postata 3 secondi prima, quindi per favore rimuovi il tuo commento e downvot. –

+0

non posso rimuovere downvote, mi dispiace. –

0

è necessario sfuggire alle citazioni interne in questo modo:

String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}"; 
3

preferisco questo, basta assicurarsi che non si dispone di un'unica citazione nella stringa

string str = "{'Id':'123','DateOfRegistration':'2012 - 10 - 21T00: 00:00 + 05:30','Status':0}".Replace("'", "\""); 
Problemi correlati