如何从.NET core 2 Web API中的JSON响应中删除\,\ r,\ n [英] How do I remove \, \r, \n from a JSON response in .NET core 2 web API

查看:454
本文介绍了如何从.NET core 2 Web API中的JSON响应中删除\,\ r,\ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在两个HTTP标头,Content-Type:application / json和Accept:application / json



我的回答如下:



Two HTTP headers are present, Content-Type:application/json and Accept:application/json

My response look like this:

"{\r\n  \"auth_token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJKLkRvZSIsImp0aSI6IjM5MDQ4MjllLTVjZWYtNGU4ZC1iYjZhLTNmYzI1NmFmMGNkYSIsImlhdCI6MTU0MTY3NjYxNSwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiTWVtYmVyIiwibmJmIjoxNTQxNjc2NjE0LCJleHAiOjE1NDE2ODM4MTQsImlzcyI6IndlYkFwaSIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTYyNDkvIn0.m25YLVNdFhqOT2YsDzTADsPG1H2yoHMDDYvCOaqwl_8\",\r\n  \"expires_in\": 7200\r\n}"



通过Postman应用程序请求回复。



Newtonsoft.Json用于序列化和格式化


Response is requested through Postman app.

Newtonsoft.Json is used to serialize and format

JsonConvert.SerializeObject(response, new JsonSerializerSettings { Formatting = Formatting.Indented });





我尝试了什么:



我试过这个:



What I have tried:

I tried this :

jwt = jwt.Replace("\"", "");





但是,这只解决了问题的一半,它只删除\。



But, that solves only half of the problem, it removes only \.

推荐答案

可能在字符串中不是任何\r或\ n,它们是调试器中的文本可视化器转换为\\\\ n的新行代码。如果您不想在json中使用新行,请尝试使用



There probably aren't any \r or \n in your string, they are new line codes that the text visualiser in the debugger is converting to "\r\n". If you don't want new lines in your json try using

Formatting.None





而不是





instead of

Formatting.Indented


这篇关于如何从.NET core 2 Web API中的JSON响应中删除\,\ r,\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆