2017-04-15 3 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 데이터를 웹 서버로 보내려고합니다. 내가 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에 포스트 본체를 통과 할 때, 그것은 저를 준다 : 유효하지 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}} 

. 무엇이 보내지고 있습니까? 왜 여분의 중괄호를 추가하고 처음 인용 부호를 제거합니까?

+0

"나에게 준다"고 말하면 "정확히"무엇입니까? 그 결과물을 얼마나 정확하게 얻었는지 설명하십시오. –

답변

0

나는 실제로 이들이 바이트 수준에서 동일한 문자열이라고 생각합니다. 차이점은 Visual Studio에서 사용자에게 표시되는 방식에 있습니다.

첫 번째 예는 구문 상 올바른 C# 리터럴 문자열로, 내부 따옴표를 이스케이프 처리하여 따옴표로 묶습니다. 실제 문자열에는 물론 백 슬래시 문자 나 시작/끝 따옴표가 포함되어 있지 않습니다. 그 (것)들은 다만 에 요구된다 C#에있는 끈.

두 번째 예제는 Visual Studio의 디버깅 도구가 개체와 그 내용을 표시하는 방법처럼 보입니다. 이 경우, 바깥 쪽 {}은 VS가 어떻게 객체인지를 알려줍니다. 그 문자는 실제 문자열에 없습니다.

그래서 다시 생각해 봅시다. IDE에서 다르게 표현한 동일한 실제 문자열입니다.

+0

VS 디버거가 특정 방법으로 문자열을 표시하는 것으로 의심됩니다. 확인해 주셔서 감사드립니다. – ShrimpCrackers