2017-04-15 112 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