2016-12-16 11 views
5

po mojej stronie serwera Mam obiekt Java, który zawiera HashMap. Chcę serializować go do JSON, zwrócić go do mojego klienta Angular2 i użyć go jako mapy/słownika.Mapa Java do JSON do mapy maszynowej

Oto klasa:

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; 
    } 

}

Oto JSON że otrzymam po stronie klienta:

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

Oto mój otrzymania Angular2 http wezwanie:

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

FileUploadResult na kliencie wygląda następująco:

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; 
    } 
} 

Używając „jak FileUploadResult” w wywołaniu http.map, spodziewałem się dostać obiekt na którym mogę zadzwonić result.getParameters().get("myKey"). Ale tak się nie dzieje. Dostaję nieokreślony obiekt, w którym jedynym połączeniem, które działa, jest result.parameters.myKey. Czy istnieje sposób, aby osiągnąć to, co chcę i rzucić obiekt JSON na FileUploadResult, w tym mapę Angular2?

+0

To samo, co http://stackoverflow.com/questions/29758765/json-to-typescript-class-instance? –

+0

@RyanCavanaugh nie, niestety, nie. Nie znam kluczy z góry. – Androidicus

Odpowiedz

6

Wynikiem wywoływania res.json() jest JavaScript, przedmiot, który można otworzyć tak:

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

Sposób, w celu opisania takiego obiektu w maszynie stosuje się interfejs (lub typu alias)

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

Jeśli chcesz przekształcić ten obiekt w swojej klasie trzeba zrobić coś takiego:

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)