1. 程式人生 > >程式碼中書寫Json字串格式

程式碼中書寫Json字串格式

說出來有點尷尬,雖然知道Json的書寫格式,但是實際在程式碼中敲一串Json字串卻會因為新增轉義字元而搞得有點頭暈,這裡特意給出文章以幫助跟我一樣解決初次接觸Json字串時頭暈的尷尬局面

其實這種局面我們要的不過是一個模板,下面給出cJson官網測試時使用的Json字串

char text1[]="{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\":       \"rect\", \n\"width\":      1920, \n\"height\":     1080, \n\"interlace\":  false,\"frame rate\": 24\n}\n}";	
	char text2[]="[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]";
	char text3[]="[\n    [0, -1, 0],\n    [1, 0, 0],\n    [0, 0, 1]\n	]\n";
	char text4[]="{\n		\"Image\": {\n			\"Width\":  800,\n			\"Height\": 600,\n			\"Title\":  \"View from 15th Floor\",\n			\"Thumbnail\": {\n				\"Url\":    \"http:/*www.example.com/image/481989943\",\n				\"Height\": 125,\n				\"Width\":  \"100\"\n			},\n			\"IDs\": [116, 943, 234, 38793]\n		}\n	}";
	char text5[]="[\n	 {\n	 \"precision\": \"zip\",\n	 \"Latitude\":  37.7668,\n	 \"Longitude\": -122.3959,\n	 \"Address\":   \"\",\n	 \"City\":      \"SAN FRANCISCO\",\n	 \"State\":     \"CA\",\n	 \"Zip\":       \"94107\",\n	 \"Country\":   \"US\"\n	 },\n	 {\n	 \"precision\": \"zip\",\n	 \"Latitude\":  37.371991,\n	 \"Longitude\": -122.026020,\n	 \"Address\":   \"\",\n	 \"City\":      \"SUNNYVALE\",\n	 \"State\":     \"CA\",\n	 \"Zip\":       \"94085\",\n	 \"Country\":   \"US\"\n	 }\n	 ]";

可以先看cJson官網測試時使用的Json字串,如果不甚明白我這裡給一個簡單的Json字串

string strRequest="{\"configInfos\":\"00\",\"ServiceReqMsg\":\"encrptyNewServiceReqMsg\"}";

c語言定義" "裡面定義字串,如果在" "裡面出現特殊字元就需要使用轉義字元\