HttpStringContent 使 JSON 无效?
HttpStringContent Making JSON Invalid?
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:
"{\"ParentId\":\"uwp@test.com\",\"ParentPrimaryId\":\"uwp@test.com\",\"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}"
但是,当我将 post 正文传递给 HttpStringContent 时,它会给我:
{{"ParentId":"uwp@test.com","ParentPrimaryId":"uwp@test.com","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。这是要发送的内容吗?为什么要添加额外的外括号并删除开头的引号?
我怀疑它们实际上是字节级别的相同字符串,不同之处在于 Visual Studio 向您显示它们的方式。
第一个示例是语法正确的 C# 文字字符串,用引号括起来,内部引号被转义。 actual 字符串当然不包含反斜杠字符或 starting/ending 引号;这些只是 表示 C# 中的字符串所必需的。
第二个示例类似于 Visual Studio 的调试工具如何显示对象及其内容。在这种情况下,外部 {}
是 VS 告诉您它是一个对象的方式;这些字符不在实际字符串中。
因此,我再次认为它们是逐字节相同的实际字符串,只是 IDE 的不同表示。
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:
"{\"ParentId\":\"uwp@test.com\",\"ParentPrimaryId\":\"uwp@test.com\",\"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}"
但是,当我将 post 正文传递给 HttpStringContent 时,它会给我:
{{"ParentId":"uwp@test.com","ParentPrimaryId":"uwp@test.com","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。这是要发送的内容吗?为什么要添加额外的外括号并删除开头的引号?
我怀疑它们实际上是字节级别的相同字符串,不同之处在于 Visual Studio 向您显示它们的方式。
第一个示例是语法正确的 C# 文字字符串,用引号括起来,内部引号被转义。 actual 字符串当然不包含反斜杠字符或 starting/ending 引号;这些只是 表示 C# 中的字符串所必需的。
第二个示例类似于 Visual Studio 的调试工具如何显示对象及其内容。在这种情况下,外部 {}
是 VS 告诉您它是一个对象的方式;这些字符不在实际字符串中。
因此,我再次认为它们是逐字节相同的实际字符串,只是 IDE 的不同表示。