Java 映射到 JSON 到 Typescript 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41188725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:05:19  来源:igfitidea点击:

Java Map to JSON to Typescript Map

jsonangulartypescript

提问by Androidicus

on my server side I have a Java object that contains a HashMap. I want to serialize it to JSON, return it to my Angular2 client and use it as a Map/Dictionary there.

在我的服务器端,我有一个包含 HashMap 的 Java 对象。我想将它序列化为 JSON,将它返回给我的 Angular2 客户端并将其用作 Map/Dictionary 那里。

Here's the class:

这是课程:

public class FileUploadResult {
    String timestamp;
    String message;
    String status;
    HashMap<String, String> parameters;

    public FileUploadResult(String status, String message, String timestamp, HashMap parameters) {
        this.status = status;
        this.message = message;
        this.timestamp = timestamp;
        this.parameters = parameters;
    }

}

}

Here's the JSON that I receive on the client side:

这是我在客户端收到的 JSON:

{"timestamp":"","message":"Test","status":"1","parameters":{"myKey":"Value","mySecondKey":"Another Value"}}

Here's my receiving Angular2 http call:

这是我收到的 Angular2 http 调用:

this.http.post(this.uploadURL, formData).map((res:Response) => res.json() as FileUploadResult).catch(this.handleError); 

FileUploadResult on the client looks like this:

客户端上的 FileUploadResult 如下所示:

export class FileUploadResult {
    status: string;
    timestamp: string;
    message: string;
    parameters: Map<string, string>;

    constructor() {
        this.parameters = new Map<string, string>();
    }

    addParameter(key: string, value: string) {
        this.parameters.set(key, value);
    }

    getParameters() {
        return this.parameters;
    }
}

By using the "as FileUploadResult" in the http.map call, I expected to get an object on where I can call result.getParameters().get("myKey"). But that's not happening. I get an unspecified object where the only call that works is result.parameters.myKey. Is there a way to achieve what I want and to cast the JSON object to the FileUploadResult including the Angular2 map?

通过在 http.map 调用中使用“as FileUploadResult”,我希望在我可以调用的地方获得一个对象result.getParameters().get("myKey")。但事实并非如此。我得到一个未指定的对象,其中唯一有效的调用是result.parameters.myKey. 有没有办法实现我想要的并将 JSON 对象转换为 FileUploadResult 包括 Angular2 地图?

回答by Nitzan Tomer

The result of calling res.json()is a javascript object which can be accessed like so:

调用的结果res.json()是一个可以像这样访问的 javascript 对象:

let json = res.json();
console.log(json["timestamp"]);
console.log(json.message);

The way to describe such an object in typescript is using an interface (or type alias):

在打字稿中描述此类对象的方法是使用接口(或类型别名):

interface JsonResponse {
    timestamp: string;
    message: string;
    status: string;
    parameters: { [name: string]: string };
}

If you want to transform this object into your class you'll need to do something like:

如果您想将此对象转换为您的类,您需要执行以下操作:

class FileUploadResult {
    status: string;
    timestamp: string;
    message: string;
    parameters: Map<string, string>;

    constructor(json: JsonResponse) {
        this.status = json.status;
        this.timestamp = json.timestamp;
        this.message = json.message;

        this.parameters = new Map<string, string>();
        Object.keys(json.parameters).forEach(key => {
            this.addParameter(key, json.parameters[key]);
        });
    }

    addParameter(key: string, value: string) {
        this.parameters.set(key, value);
    }

    getParameters() {
        return this.parameters;
    }
}

(code in playground)

操场上的代码