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.
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:
{"timestamp":"","message":"Test","status":"1","parameters":{"myKey":"Value","mySecondKey":"Another Value"}}
Here's my receiving Angular2 http call:
this.http.post(this.uploadURL, formData).map((res:Response) => res.json() as FileUploadResult).catch(this.handleError);
FileUploadResult on the client looks like this:
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?