简体   繁体   English

Angular Http JSON 响应映射

[英]Angular Http JSON response Mapping

I am trying to map a Http JSON Response to a Custom Interface in Angular / typescript. I am trying to map a Http JSON Response to a Custom Interface in Angular / typescript. I have tried it in several ways but have not made it yet.我已经尝试了几种方法,但还没有成功。 The JSON object is not correctly mapped to the interface. JSON object 未正确映射到接口。 The map attribute stays "undefined". map 属性保持“未定义”。 If I print the data directly, the JSON data is output correctly - the problem is that I don't know how to access it.如果我直接打印数据,JSON 数据是正确的 output - 问题是我不知道如何访问它。 Here is my code:这是我的代码:

export interface IMap<T> {
map: Map<string, Array<T>>;
}

The JSON answer looks like this. JSON 答案看起来像这样。 It is Map< String,List<?它是 Map<String,List<? >> in Java. >> 在 Java 中。

{
    "somenumbers": [
        20,
        40
    ],
    "somemorenumbers": [
        71,
        111
    ]
}

Now I tried to map it the following way:现在我尝试通过以下方式 map 它:

public getValues(
    paramList: Array<string>
): Observable<IMap<any>> {
    const url = `url`;
    let params = new HttpParams();
    for (let s of paramList) {
        params = params.append("params", s);
    }

    return this.http.get<IMap<any>>(url, { params });
}

In the configservice I subscribe to the Method.在配置服务中,我订阅了方法。 How do I map the Response correctly so that the attribute map in data isn't undefined and can be accessed correctly?如何正确 map 响应,以便数据中的属性 map 不是未定义的并且可以正确访问?

this.configService
        .getValues(["somenumbers", "somemorenumbers"])
        .subscribe((data: IMap<any>) => {
            //outputs the JSON Data as Object{somenumbers: Array(2), somemorenumbers: Array(2), map: Map(0)}
            console.error(data); 
            console.error(data.map);//map is undefined => ERROR
        });

As you can see the map attribute is undefined.如您所见,map 属性未定义。 It is just a "map: Map(0)".它只是一个“地图:地图(0)”。 Now... - How do I get the JSON stuff into the export interface?现在... - 我如何将 JSON 的东西放入导出界面? The map attribute should be filled with the associated values. map 属性应填充相关值。 I appreciate any help: :)我很感激任何帮助::)

If I understood correctly you're expecting that by adding <IMap<any>> to the get call it will then return you the response mapped to IMap .如果我理解正确,您期望通过将<IMap<any>>添加到 get 调用中,它将返回映射到IMap的响应。 It doesn't, check this issue.不会的,看看这个问题。

What you can do instead is use rxjs map to map the response yourself like so:您可以做的是使用 rxjs map到 map 自己的响应,如下所示:

return this.http.get<IMap<any>>(url, { params }).pipe(
  map((response) => {
    // map the response here
  })
);

I realized that I actually don't need the export interface and changed the code to the following.我意识到我实际上不需要导出接口并将代码更改为以下。 It took a while for me to get that xy is in ts the same as x["y"].我花了一段时间才知道 xy 在 ts 中与 x["y"] 相同。 Via response[parameter] I can access the attributes of the response Object dynamically - exactly what I needed.通过 response[parameter] 我可以动态访问响应 Object 的属性——这正是我所需要的。

public getValues(
    paramList: Array<string>
): Observable<Map<string, Array<any>>> {
    const url = `url`;
    let params = new HttpParams();
    for (let s of paramList) {
        params = params.append("params", s);
    }

    return this.http
        .get<any>(url, {
            params
        })
        .pipe(
            map(response => {
                let toReturn = new Map<string, any[]>();
                for (let parameter of paramList) {
                    toReturn.set(parameter, response[parameter]);
                }
                return toReturn;
            })
        );
}

The mapping works now.映射现在有效。 The JSON answer is still the same as in the question above. JSON 的答案仍然与上述问题相同。

this.configService
        .getValues(["somenumbers", "somemorenumbers"])
        .subscribe((data: Map<string, any[]>) => {

            console.error(data);
        });

Thanks for the help and links @M Mansour !感谢@M Mansour 的帮助和链接!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM