I am trying to read the following api json response in typescript. I have tried by defining interfaces as shown below. But when reading the values of phone number using loops I am getting value as undefined.
Could some one please help me to parse the below json file in most efficient way.
for(let i=0; i < custdetails.length; i++){
phnnumber[i] = custdetails[i].phoneNumber;
}
export interface Address {
streetAddress: string;
city: string;
}
export interface PhoneNumber {
type: string;
number: string;
}
export interface custdetails{
name: string;
age: number;
address: Address[];
phoneNumber: PhoneNumber[];
}
[
{
"name": "Test name1",
"age": 30,
"address": {
"streetAddress": "2nd test street",
"city": "London"
},
"phoneNumber": [{
"type": "home",
"number": "111 111-1111"
},
{
"type": "fax",
"number": "222 222-2222"
}
]
},
{
"name": "Test name2",
"age": 30,
"address": {
"streetAddress": "3rd test street",
"city": "Sydney"
},
"phoneNumber": [{
"type": "home",
"number": "888 888-8888"
},
{
"type": "fax",
"number": "999 999-9999"
}
]
}
]
rootobjarray? If it's the result of parsing the JSON shown, it would be an array ofcustdetails. That object'sphoneNumberproperty is an array ofPhoneNumberobjects (and should probably be namedphoneNumber**s**. Sophnnumber[i]will be an array ofPhoneNumberobjects. It's not clear what's undefined...custdetailstoCustdetails.. C in interface to be capital.. I believe you mistyped it