首页 > 解决方案 > 角度未正确映射字典对象

问题描述

我正在尝试将 json 字符串映射到 Angular 中的自定义对象。我认为我的问题在于将字典映射到 Angular 中的 Map。这是正在发送的对象的结构:

public class GeneratedConfiguration
{
    public List<ServiceEnvironmentConfiguration> Configurations { get; set; } =
        new List<ServiceEnvironmentConfiguration>();

    public string Service { get; set; }
}

public class ServiceEnvironmentConfiguration
{
    public string Environment { get; set; }

    public SortedDictionary<string, SourcedPropertyValue> Properties { get; set; } =
        new SortedDictionary<string, SourcedPropertyValue>();
}

public class SourcedPropertyValue
{
    public string SourceConfiguration { get; set; }
    public object Value { get; set; }
}

这是我在 Angular 中的模型的结构:

export interface Configurations {
    Service: string;
    Configurations: Configuration[];
}

export interface Configuration {
    Environment: string;
    Properties: Map<string, SourcedPropertyValue>;
}

export interface SourcedPropertyValue {
    SourceConfiguration: string;
    Value: any;
}

这就是我试图映射它的方式:

getConfigs(service: string): Observable<Configurations> {
    return this.http.get<Configurations>(this.url).pipe(
        catchError(this.handleError)
    );
}

它通过环境变量正确映射,但属性始终列为未定义。所以我认为问题可能出在我的地图上?谁能告诉我如何更改我的模型以正确映射字典部分?

标签: angulartypescript

解决方案


推荐阅读