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?
We can convert a Map to JSON object using the toJSONString() method(static) of org. json.
TypeScript Hashmap defines the type of key and value that it holds. Hashmap in TypeScript has a Generic type as 'Map' and is modified frequently for better performance. Hashmap even protects with type safety. Regarding the TypeScript performance, object literals are slow if a user modifies the TypeScript Hashmap.
A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value. We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format.
Use Map type and new keyword to create a map in TypeScript. let myMap = new Map<string, number>(); To create a Map with initial key-value pairs, pass the key-value pairs as an array to the Map constructor.
The result of calling res.json()
is a javascript object which can be accessed like so:
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)
class FileUploadResult {
parameters: Record<string, string> = {};
addParameter(key: string, value: string) {
this.parameters[key] = value;
}
}
You can use it this way
const abc = new FileUploadResult();
abc.addParameter('hi', 'hello');
console.log(abc.parameters); // will log {hi: "hello"}
https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With