How can I convert a JSON string to a nested interface type and validate it?
Although my model is more complex, here is an example:
export interface User = {
name: Field;
surname: Field;
};
export interface Field = { icon: string; text: string; visibility: boolean };
export interface Users = User[]
The conversion could be represented as:
export type user = {
name: field;
surname: field;
};
export type field = { icon: string; text: string; visibility: boolean };
export type users = user[]
It could also be implemented using classes.
Here is a sample JSON data:
[
{
"name": { "text": "David", "icon": "icon1.png", "visibility": true },
"surname": { "text": "Smith", "icon": "icon2.png", "visibility": true }
},
{
"name": { "text": "Arthur", "icon": "icon3.png", "visibility": true },
"surname": { "text": "L.", "icon": "icon6.png", "visibility": true }
},
{
"name": { "text": "Anthony", "icon": "icon1.png", "visibility": false },
"surname": { "text": "Isaacson", "icon": "icon2.png", "visibility": true }
},
{
"name": { "text": "Mike", "icon": "icon3.png", "visibility": true },
"surname": { "text": "Jobs", "icon": "icon5.png", "visibility": false }
}
]
Update:
Here is an example illustrating why Chithambara's method may not be valid: Playground