繁体   English   中英

从Rest API检索响应

[英]Retrieve the response from the Rest API

我有REST API,它返回JSON响应,例如

[{
    "AccountID": "adm",
    "CounterSeq": "024",
    "Year": "17"
}]

我需要检索响应

 var response_EndPoint = await client_EndPoint.GetAsync(EndPoint_URL);
 var projectName = await response_EndPoint.Content.ReadAsAsync<APIResponseModel[]>();

模型类看起来像

public class APIResponseModel
{
    public string AccountID { get; set; }
    public string CounterSeq { get; set; }
    public string Year { get; set; }
}

我需要构造类似于“ adm-024-17”的字符串。 如何转换字符串中的响应?

如果要从服务调用中获取APIResponseModel的实例,则只需将值格式化为所需的字符串即可:

假设它看起来像是一个APIResponseModel实例数组,那么您只需要遍历集合并将它们格式化为字符串...

foreach(APIResponseModel response in projectName)
{
    string serviceResponse = string.Format("{0}-{1}-{2}", response.AccountID, response.CounterSeq, response.Year);
    Console.WriteLine(serviceResponse);
}

您可以向类添加只读属性。 这是假设您已反序列化JSON字符串。

public class APIResponseModel
{
    public string AccountID { get; set; }
    public string CounterSeq { get; set; }
    public string Year { get; set; }

    public string getAllValues
    {
        get
        {
            return AccountID + "-" + CounterSeq + "-" + Year;
        }
    }

    // and/or

    public override string ToString()
    {
        return AccountID + "-" + CounterSeq + "-" + Year;
    }
}

暂无
暂无

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

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