2012-11-21 19 views
10

我要存儲在一個字符串變量如何在代碼中編寫JSON字符串值?

{ 「ID」 以下字符串: 「123」, 「DateOfRegistration」: 「2012-10-21T00:00:00 + 05:30」,「狀態「:0}

這是我使用的代碼..

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

..但它顯示的錯誤..

+2

這是很明顯的,但在接下來的問題應該具體什麼樣的錯誤它的顯示。這是幫助那些想要幫助你的寶貴信息。 – Guillaume

+0

您應該選擇一個答案和/或upvote有幫助的答案。 – William

回答

21

你必須要做到這一點

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


see this for reference
此外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

你不應該逃避最後的報價;) – Brian

+0

haha​​haha完全.. :) – Freak

2

你要逃避這樣的串中的引號:

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

的確,@MikeBarnes,但是如果你仔細觀察時間戳,你會發現我的答案在3秒之前發佈 - 所以請刪除你的評論,然後倒下。 –

+0

不能刪除downvote,對不起。 –

0

你需要躲避內引號,像這樣:

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

我喜歡這個,只是確保你沒有單引號的字符串

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