如何在C#中获得JSON中没有任何空格的WebResponse?

zbdgwd5y  于 5个月前  发布在  C#
关注(0)|答案(1)|浏览(74)

我尝试使用WebRequest在C#中调用一个API端点,然后使用WebResponse来获取响应。我正在接收JSON,但JSON响应中充满了大量空格,我想摆脱这些空格。
我尝试使用格式化来去除空格。Formatting.NoneFormatting.Indented都提供相同的JSON输出。
这是我的代码的一个例子:

System.Net.WebResponse resp = req.GetResponse();
string content;

using(System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream())) 
{
    content = sr.ReadToEnd();
}

dynamic parsedJson = JsonConvert.DeserializeObject(content);

content = JsonConvert.SerializeObject(parsedJson, Newtonsoft.Json.Formatting.None);

字符串
这种形式的JSON字符串由上面的代码返回。

"{\n \t\"Success\": \"true\",\n \t\"Status\": 200,\n \t\"SuccessPnrs\": null,\n \t\"RepeatedPnrs\": [\n \t\t\"1184017\"\n \t]\n }"


预期的JSON显示在此处:

{"Success": "true","Status": 200,"SuccessPnrs": null,"phone": ["9568523624"]}

gk7wooem

gk7wooem1#

如果只是关于\n \t个字符,我会简单地使用string.Replace(“\t”,“”)和string.Replace(“\n”,“”)

相关问题