简体   繁体   中英

Retrieve the response from the Rest API

I have the REST API which returns the JSON response like

[{
    "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". 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:

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...

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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