我从第三方API获取一些数据。在响应中,我得到一个动态的location_info对象
{
"country_code3": "SE",
"country_name": "Sweden",
"city_name": "Malmo",
"latitude": 86.69,
"longitude": 173.0551
}
现在我想获取country_name
并使用AutoMapper将其映射到属性Country
。为此,我想将上面提到的动态JSON反序列化为LocationInfo
模型:
public class LocationInfo
{
public string Country_Name { get; set; }
}
然后AutoMapper将Country_Name
映射到Country
。这是我尝试过的:
.ForMember(dest => dest.Country, opts => opts.MapFrom(src => JsonConvert.DeserializeObject<LocationInfo>(src.LocationInfo).Country_Name);
但它没有用,我得到以下异常:
表达式树可能不包含动态操作。
当您需要在映射中执行某些方法时,可以使用ResolveUsing
方法。对于您的情况,它将是:
.ForMember(dest => dest.Country, opts => opts.ResolveUsing(src => JsonConvert.DeserializeObject<LocationInfo>(src.LocationInfo).Country_Name);