c#如何迭代API URL调用列表

本文关键字:API URL 调用 列表 迭代 何迭代 | 更新日期: 2023-09-27 18:11:41

我有这个json数据,并把它放入一个列表:

{
"date": "2016-08-26",
"time_of_day": "14:19",
"request_time": "2016-08-26T14:19:59+01:00",
"station_name": "Derby",
"station_code": "DBY",
"departures": {
  "all": [
    {   
       "service": "22152000",
    },
    {
       "service": "22150000",
    },
    {
       "service": "22180008",
    }
  ]
}
}
dynamic content = JsonConvert.DeserializeObject(json);
JArray items = new JArray();
items.Add(service["service"]);
int serviceLength = items.Count;
在此之后,我想在url中对项目列表中的每个单独项目进行API调用。这是我目前所看到的:
for (int i = 0; i < items.Count; i++)
{
string moreJson = get_web_content("http://transportapi.com/v3/uk/train/service/" + items[i] + "/" + appID + "/" + appKey);
}

谢谢你的帮助!

c#如何迭代API URL调用列表

你能试试这样吗?

if (content.departures != null)
        {
            var departureString = content.departures.ToString();
            departureString = Regex.Replace(departureString, @"[^'d|':]", string.Empty);
            var items = departureString.Split(':');
            if (items != null && items.Length > 0)
            {
                for (int i = 0; i < items.Count; i++)
                {
                    if (!string.IsNullOrEmpty(items[i]))
                    string moreJson = get_web_content("http://transportapi.com/v3/uk/train/service/" + items[i] + "/" + appID + "/" + appKey);
                }
            }
        }