F
F
Felino2018-07-31 15:58:58
Angular
Felino, 2018-07-31 15:58:58

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};
            }
          );
        }
      )
    );
  }

json structure
{
"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"
    }]
  }
]
}

model structure
export class People {
  constructor(
    public firstName: string,
    public lastName: string,
    public avatar: string,
    public id?: number
  ) {
  }
}

Complete information structure
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
  ) {
  }
}

What side tell me! in which direction to dig?
I understand that in order to get the full information of a particular object, you need to transfer its ID?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
Qairat, 2018-09-13
@Qairat

Look
I usually do this myself, and I do everything through the Interface, it's more convenient.
In the example, I did parsing for People.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question