Answer the question
In order to leave comments, you need to log in
How to parse data received from Json?
Good afternoon!
I receive JSON from the server.
Tell me how to properly extract data from it into the collection now
I'm doing it like this at the moment!
getPeople(): Observable<People[]> {
return this.http.get<People[]>('http://localhost:3000/global').pipe(
map
(
data => {
let userlist = data['general'];
return userlist.map
(
function (user: any) {
console.log(user.firstName);
return {first_name: user.firstName, last_name: user.lastName, ava: user.avatar};
}
);
}
)
);
}
{
"global":[
{
"general": [{
"firstName": "Liana",
"lastName": "Crooks",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/kevinoh/128.jpg"
}],
"job": [{
"company": "Ledner, Johnson and Predovic",
"title": "Investor Functionality Coordinator"
}],
"contact": [{
"email": "[email protected]",
"phone": "(895) 984-0132"
}],
"address": [{
"street": "1520 Zemlak Cove",
"city": "New Devon",
"zipCode": "42586-7898",
"country": "Guinea-Bissau"
}]
},
{
"general": [{
"firstName": "Deontae",
"lastName": "Dare",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/andysolomon/128.jpg"
}],
"job": [{
"company": "D'Amore, Dicki and Borer",
"title": "International Applications Consultant"
}],
"contact": [{
"email": "[email protected]",
"phone": "1-615-843-3426 x600"
}],
"address": [{
"street": "65901 Glover Terrace",
"city": "Alden ton",
"zipCode": "57744-4248",
"country": "Kenya"
}]
}
]
}
export class People {
constructor(
public firstName: string,
public lastName: string,
public avatar: string,
public id?: number
) {
}
}
export class FullModel {
constructor(
public firstName: string,
public lastName: string,
public avatar: string,
public company: string,
public title: string,
public email: string,
public phone: string,
public street: string,
public city: string,
public zipCode: string,
public country: string,
public id?: number
) {
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question