I am receiving this JSON structure from my asp.net core
API:
{
"contentType": null,
"serializerSettings": null,
"statusCode": null,
"value": {
"productName": "Test",
"shortDescription": "Test 123",
"imageUri": "https://bla.com/bla",
"productCode": null,
"continuationToken": null
}
}
Below is the typescript function I am using to call the API and retrieve the above response:
public externalProduct: ProductVM;
getProductExternal(code: string): Observable<ProductVM> {
return this.http.get("api/product?productCode=" + code)
.map((data: ProductVM) => {
this.externalProduct = data; //not working...
console.log("DATA: " + data);
console.log("DATA: " + data['value']);
return data;
});
}
This is how the ProductVM
interface looks like:
export interface ProductVM {
productName: string;
shortDescription: string;
imageUri: string;
productCode: string;
continuationToken: string;
}
The issue I am facing is that I am unable to deserialize it to a ProductVM
object. The console logs only display [object Object]
How can I properly map the contents of the value
in my JSON response to a ProductVM
object?
Is it correct to assume that data
is a ProductVM
in the map function? Despite trying various approaches, I have not been able to achieve the desired outcome!
I am uncertain whether I should instruct angular
to automatically map the value array in the JSON response to a ProductVM
object or if I should modify the ProductVM
class by providing a constructor (it's currently an interface) and manually extract the specific values from the JSON?