简体   繁体   English

如何在 C# 中清除来自 API 的字符串

[英]How to clean string coming from API in C#

I am using C#我正在使用 C#

in my code, I call API在我的代码中,我调用 API

This API returns a string.这个 API 返回一个字符串。

But the problem is when I read the API it comes with backslashes and quotations.但问题是当我阅读 API 时,它带有反斜杠和引号。

So if the returned value is Black\White's colors所以如果返回值为Black\White's colors

It becomes "\"Black\\\\White's colors\""它变成"\"Black\\\\White's colors\""

or empty string is "\"\""或空字符串是"\"\""

I wonder if there is a way to parse string the in a right way.我想知道是否有办法以正确的方式解析字符串。

here is my code这是我的代码

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

        string str = client.GetStringAsync(url).Result;

Like this像这样

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));

    string str = await client.GetStringAsync(url);

    //choose this if using NewtonsoftJson 
    string parsedWithNewtonsoft = JsonConvert.DeserializeObject<string>(str);

    //choose this if using STJ
    string parsedWithSystemTextJson = JsonSerializer.Deserialize<string>(str);

Choose one of the last two lines according to your preferred installed Json parser (manage nuget packages for the project and install either newtonsoft or system.text.json)根据您首选安装的 Json 解析器选择最后两行之一(管理项目的 nuget 包并安装 newtonsoft 或 system.text.json)

Don't use Result;不要使用结果; use async proper (which means you have to make your method calls async all the way up the chain).正确使用 async (这意味着你必须让你的方法调用 async 一路向上)。 If you strive to turn all your async calls synchronous then your app will spend a lot of tine sitting around doing nothing, waiting for network io to finish, when that time could be put to doing other useful work如果您努力使所有异步调用同步,那么您的应用程序将花费大量时间无所事事,等待网络 io 完成,那时可以将时间用于做其他有用的工作

Also, HttpClient is not supposed to be created anew every time you want to use it.此外,每次您想使用 HttpClient 时都不应该重新创建它。 Either configure your DI with services.AddHttpClient();使用services.AddHttpClient();配置您的 DI passing in options as necessary or if you aren't using DI/not writing a service of your own, you can get some ideas on how to use HttpClientFactory to manufacture HttpClients for you form this question - you may even be able to configure the default request headers as part of the manufacture so you don't need to set them in code 根据需要传入选项,或者如果您不使用 DI/不编写自己的服务,您可以从这个问题中获得一些关于如何使用 HttpClientFactory 为您制造 HttpClients 的想法 - 您甚至可以配置默认值请求标头作为制造的一部分,因此您无需在代码中设置它们

Finally, I should point out that I think there's a chance you're doing a lot more work than you need to if your API you're using publishes a swagger document;最后,我应该指出,如果您使用的 API 发布了 swagger 文档,我认为您做的工作可能比您需要的多得多; if it does you can use a tool like AutoRest (or the "add rest client" feature of visual studio, which uses AR), NSwag, WebApiClientGen etc and tell it "here is the api I want to use" and it'll make everything you need to call the api and return c# objects;如果是这样,您可以使用 AutoRest(或使用 AR 的 Visual Studio 的“添加 rest 客户端”功能)、NSwag、WebApiClientGen 等工具并告诉它“这是我想使用的 api”调用 api 并返回 c# 对象所需的一切; you don't need to code any of this low level stuff, pushing data around and parsing responses, yourself您无需自己编写任何低级代码、推送数据和解析响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM