简体   繁体   English

从Rest API检索响应

[英]Retrieve the response from the Rest API

I have the REST API which returns the JSON response like 我有REST API,它返回JSON响应,例如

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

I need to retrieve the response 我需要检索响应

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

The model class looks like 模型类看起来像

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

I need to construct the String which should be like "adm-024-17". 我需要构造类似于“ adm-024-17”的字符串。 How can I convert the response in the string? 如何转换字符串中的响应?

If you are getting an instance of your APIResponseModel back from your service call, you just simply need to format the values into the string that you want: 如果要从服务调用中获取APIResponseModel的实例,则只需将值格式化为所需的字符串即可:

Assuming, as it appears, it can come back as an array of APIResponseModel instances, then you just need to loop through the collection and format them as strings... 假设它看起来像是一个APIResponseModel实例数组,那么您只需要遍历集合并将它们格式化为字符串...

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

You can add a read only property to the class. 您可以向类添加只读属性。 This is assuming you have deserialized the JSON string. 这是假设您已反序列化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