I have a json file named products.json which contains a list of products with their id, name, quantity, description and url of images relating to this product.
Example of the JSON file:
[
{
"id":1,
"name": "APPLE IPHONE 13 256GO",
"desc": "some desc ...",
"price": 1599.0,
"qty": 15,
"imgUrls": "../Images/iphone-13"
},
{
"id":2,
"name": "GALAXY NOTE10",
"desc": "some desc ...",
"price": 200.0,
"qty": 10,
"imgUrls": "../Images/galaxy"
},
{
"id":3,
"name": "APPLE IPHONE 12 128GO",
"desc": "some desc ...",
"price": 1599.0,
"qty": 15,
"imgUrls": "../Images/iphone-12"
}
]
Also, I have the Product object below:
export class Product {
constructor(
public id: number,
public name: string,
public desc: string,
public price: number,
public qty: number,
public imgUrls: string[]
) {}
}
I want to create an array of type Product, which contains all my products imported from the json file.
I know that by adding the products manually it will be like this.
products: Product[] = [
new Product(1,
"APPLE IPHONE 13 256GO",
"some desc...",
1599.0,
15,
["../Images/iphone-13"]),
new Product(2,
"GALAXY NOTE10",
"some desc...",
200.0,
10,
["../Images/galaxy"]),
new Product(3,
"APPLE IPHONE 12 128GO",
"some desc ...",
1599.0,
15,
["../Images/iphone-12"])
];
but i want to create this object using the json file.
Thank you so much for yur help.
Sorry if i m not clear, you can mention that in comments so i can edit my question.