2017-04-15 117 views
0
httpClient = new HttpClient(); 
stringContent = new HttpStringContent(postBody, UnicodeEncoding.Utf8, "application/json"); 
httpResponse = await httpClient.PostAsync(uri, stringContent); 
String responseString = await httpResponse.Content.ReadAsStringAsync(); 

編寫UWP應用程序並嘗試將JSON數據發送到Web服務器。在我對象序列化到JSON postBody = JsonConvert.SerializeObject(parentModel);另一種方法,我得到有效的JSON:HttpStringContent使JSON無效?

"{\"ParentId\":\"[email protected]\",\"ParentPrimaryId\":\"[email protected]\",\"ParentPassword\":\"n78mG2LB18ANtzr7gd2X/fILNELjbjOMuTWbhWoDvcg=\",\"ParentFirstName\":\"Bill\",\"ParentLastName\":\"Gates\",\"AddChildDistrictId\":\"\",\"RemoveChildDistrictId\":\"\",\"ParentToken\":null,\"ParentDistrictId\":\"\",\"ParentChildDistricts\":\"\",\"AppPlatform\":\"Windows 10.0.15063.138\",\"AppVersion\":10000,\"ParentAccountStatus\":1,\"ParentStatusCode\":0,\"ParentFailedSignInAttempt\":0}" 

然而,當我通過後身體HttpStringContent,它給了我:

{{"ParentId":"[email protected]","ParentPrimaryId":"[email protected]","ParentPassword":"n78mG2LB18ANtzr7gd2X/fILNELjbjOMuTWbhWoDvcg=","ParentFirstName":"Bill","ParentLastName":"Gates","AddChildDistrictId":"","RemoveChildDistrictId":"","ParentToken":null,"ParentDistrictId":"","ParentChildDistricts":"","AppPlatform":"Windows 10.0.15063.138","AppVersion":10000,"ParentAccountStatus":1,"ParentStatusCode":0,"ParentFailedSignInAttempt":0}} 

這是無效的JSON 。這是什麼被髮送?爲什麼它會添加額外的大括號並刪除開頭的引號?

+0

當你說「它給了我」時,它到底是什麼?請解釋你如何得到那個輸出。 –

回答

0

我懷疑這些實際上是字節級別的字符串,區別在於Visual Studio如何將它們顯示給您。

第一個示例是一個語法正確的C#文字字符串,用帶引號的引號括起來,內部引號已被轉義。 實際字符串當然不包含反斜槓字符或開始/結束引號;那些只需要就代表這個C#中的字符串。

第二個示例看起來像Visual Studio的調試工具如何顯示對象及其內容。在這種情況下,外部{}是VS如何告訴你它是一個對象;這些字符不在實際的字符串中。

再次,我認爲它們是字節對於字節相同的實際字符串,只是由IDE表示不同。

+0

我懷疑是VS調試器只是以某種方式顯示字符串。感謝您的確認。 – ShrimpCrackers