On the server side, I have a Java object that includes a HashMap. My goal is to serialize it into JSON, send it back to my Angular2 client, and utilize it as a Map/Dictionary there.
The class structure is as follows:
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;
}
}
Upon receiving the JSON on the client side, it looks like this:
{"timestamp":"","message":"Test","status":"1","parameters":{"myKey":"Value","mySecondKey":"Another Value"}}
My Angular2 http call for handling this data is as follows:
this.http.post(this.uploadURL, formData).map((res:Response) => res.json() as FileUploadResult).catch(this.handleError);
Implementation of FileUploadResult in the client-side code appears as:
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;
}
}
After using "as FileUploadResult" in the http.map call, the expectation was to access
result.getParameters().get("myKey")
. However, currently only result.parameters.myKey
is functioning. Is there a way to correctly cast the JSON object to FileUploadResult along with an Angular2 map?