泛型方法Json解析

我有这个通用方法,用于执行Post请求,然后像这样解析响应。


private async Task<object> PostAsync<T1,T2>(string uri, T2 content)

{

    using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri))

    {

        var json = JsonConvert.SerializeObject(content);

        using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))

        {

            requestMessage.Content = stringContent;


            HttpResponseMessage response = await _client.SendAsync(requestMessage);

            if (response.IsSuccessStatusCode)

            {

                _logger.LogInformation("Request Succeeded");


                T1 responseModel = JsonConvert.DeserializeObject<T1>(await response.Content.ReadAsStringAsync());

                return  responseModel;

            }

            else

            {

                return await GetFailureResponseModel(response);


            }

        }

    }

}

现在的问题是一些 Post 请求响应在SnakeCase中,而其他在CamelCase中。我该如何解决这个问题。


慕莱坞森
浏览 87回答 1
1回答

互换的青春

鉴于您在编译时知道什么时候知道 snake_case 以及什么时候需要默认策略,您可以这样做:private Task<object> PostAsync<T1, T2>(string uri, T2 content){&nbsp; &nbsp; return PostAsync<T1, T2>(uri, content, new DefaultNamingStrategy());}private async Task<object> PostAsync<T1, T2>(string uri, T2 content, NamingStragy namingStrategy){&nbsp; &nbsp; using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var json = JsonConvert.SerializeObject(content);&nbsp; &nbsp; &nbsp; &nbsp; using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requestMessage.Content = stringContent;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpResponseMessage response = await _client.SendAsync(requestMessage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.IsSuccessStatusCode)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _logger.LogInformation("Request Succeeded");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var deserializerSettings = new JsonSerializerSettings&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContractResolver = new DefaultContractResolver&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NamingStrategy = namingStrategy&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T1 responseModel = JsonConvert.DeserializeObject<T1>(await response.Content.ReadAsStringAsync(), deserializerSettings);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return responseModel;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return await GetFailureResponseModel(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}所以,当你需要默认策略时:await PostAsync<Some1, Some2>(uri, some2Content);并且,当你需要 snake_case 时:await PostAsync<Some1, Some2>(uri, some2Content, new SnakeCaseNamingStrategy());
打开App,查看更多内容
随时随地看视频慕课网APP