简体   繁体   English

来自API C#的HttpContent ReadAsAsync映射数据

[英]HttpContent ReadAsAsync map data coming from api c#

I am doing server to server communication and I am getting data back from my api. 我正在进行服务器到服务器的通信,并且正在从api中获取数据。 I using Http web client to do that. 我使用Http Web客户端来做到这一点。 This is my code 这是我的代码

public async Task<List<Report>> GetReports(string tokenType, string token, int page, int count)
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, token);


    var builder = new UriBuilder(ApiEndPointBase + "api/" + ApiVersion + "/LibraryDocument");
    builder.Port = -1;
    var query = HttpUtility.ParseQueryString(builder.Query);
    query["page"] = page.ToString();
    query["count"] = count.ToString();
    builder.Query = query.ToString();
    string url = builder.ToString();

    var result = await client.GetAsync(url);


    if (!result.IsSuccessStatusCode)
        throw new Exception("");

    List<Report> reports = new List<Report>();
    await result.Content.ReadAsAsync<List<Report>>().ContinueWith(response =>
   {
       reports = response.Result;
   });
    return reports;
}

The issue I am having is that I am getting data from server in this format 我遇到的问题是我正在以这种格式从服务器获取数据

public class Report 
{
    public int pK_LibraryDocument { get; set; }
    public string fileName { get; set; }
    public List<string> AvailableForModules { get; set; }
}

But I want data like this 但是我想要这样的数据

public class Report 
{
    public int id{ get; set; }
    public string fileName { get; set; }
    public List<string> AvailableForModules { get; set; }
}

I would probably have other variable name changes as well. 我可能还会更改其他变量名。 The reason for that is that I would have multiple data sources with same data but the format or name would be different. 这样做的原因是,我将有多个具有相同数据的数据源,但是格式或名称将不同。 So I want to have a centralize naming for my self. 因此,我想对自己进行集中命名。

Is it possible in a not expensive (time consuming) way. 是否可能以不昂贵(耗时)的方式。

JSON.NET that is used by default for deserialization supports JsonProperty attribute for adjusting JSON field name: 默认情况下用于反序列化的JsonProperty支持JsonProperty属性,用于调整JSON字段名称:

public class Report 
{
    [JsonProperty("pK_LibraryDocument")]
    public int id { get; set; }

    public string fileName { get; set; }
    public List<string> AvailableForModules { get; set; }
}

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

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