我有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”的字符串。如何在字符串中转换响应?
答案 0 :(得分:1)
如果您从服务电话中获得APIResponseModel
的实例,则只需将值格式化为您想要的字符串:
假设,它看起来像是APIResponseModel
个实例的数组,那么你只需要遍历集合并将它们格式化为字符串......
foreach(APIResponseModel response in projectName)
{
string serviceResponse = string.Format("{0}-{1}-{2}", response.AccountID, response.CounterSeq, response.Year);
Console.WriteLine(serviceResponse);
}
答案 1 :(得分:1)
您可以向该类添加只读属性。这假设你已经反序列化了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;
}
}