C# 从索引数组动态填充mvvm模型

C# 从索引数组动态填充mvvm模型,c#,xamarin,mvvm,xamarin.forms,xamarin.ios,C#,Xamarin,Mvvm,Xamarin.forms,Xamarin.ios,GetAirports允许我填写\u airports,如果它未编制索引,这意味着如果我使用\u airport[0] 但是,使用这个公共API链接获取机场时,会使用一个带有对象的索引数组。如何在不知道实际索引名称的情况下动态地填充\u机场和提取它们 public async Task<List<Airports>> GetAirports() { string url = _BASEURL; if( _airports == null)

GetAirports
允许我填写
\u airports
,如果它未编制索引,这意味着如果我使用
\u airport[0]

但是,使用这个公共API链接获取机场时,会使用一个带有对象的索引数组。如何在不知道实际索引名称的情况下动态地填充
\u机场
提取它们

public async Task<List<Airports>> GetAirports()
{
    string url = _BASEURL;
    if( _airports == null)
        _airports = await GetAsync<List<Airports>>(url);

    return _airports;
}
从API

"AAL":{
    "name":"Aalborg Airport",
    "country":"DK",
    "timeZone":"DK",
    "latitude":"570535N",
    "longitude":"0095100E"
},
"AAR":{...}

下面是一个基于API返回的数据格式的解决方案

向模型中添加一个附加属性以保存键/代码,如
AAL

public class Airport { //<-- note the name change of the model
    public string Code { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public string TimeZone { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
}

有一个变通方法是件好事,但是有没有办法在实际代码中保持API格式相同?我绝对可以使用这种变通方法though@Wanjia然后只需返回
字典
。我建议使用变通方法,因为您在提供的示例中使用了
List
"AAL":{
    "name":"Aalborg Airport",
    "country":"DK",
    "timeZone":"DK",
    "latitude":"570535N",
    "longitude":"0095100E"
},
"AAR":{...}
public class Airport { //<-- note the name change of the model
    public string Code { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public string TimeZone { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
}
public async Task<List<Airport>> GetAirportsAsync() { //<-- note the name change
    string url = _BASEURL;
    if( _airports == null) {
        var data = await GetAsync<Dictionary<string, Airport>>(url);
        _airports = data.Select(_ => {
            var code = _.Key;
            var airport = _.Value;
            airport.Code = code;
            return airport;
        }).ToList();
    }

    return _airports;
}