1

In angular application I have called the API and read the Json object inconsole .But inside the Json object there is string and array.Now I have to parse the array from the object incosole.

The object I have got in console is:

{
"Drone": {
    "Droneid": 1001,
    "latlong": [
        {
            "lat": 12.989839,
            "lon": 80.198822
        },
        {
            "lat": 13.051832,
            "lon": 80.194480
        },
        {
            "lat": 13.038453,
            "lon": 80.227442
        },
        {
            "lat": 13.009018,
            "lon": 80.242550
        },
        {
            "lat": 12.976903,
            "lon": 80.237056
        },
        {
            "lat": 12.956829,
            "lon": 80.193107
        },
        {
            "lat": 12.980917,
            "lon": 80.150531
        },
        {
            "lat": 13.007680,
            "lon": 80.149158
        },
        {
            "lat": 13.043805,
            "lon": 80.154651
        }
    ]
}
}

But I have to parse latlong array from the object.

Dashboard.service.ts

mapping(token){
  let httpOptions = {
    headers: new HttpHeaders({
    'Content-Type': 'application/json',
      'Authorization': 'Token ' + token
    })
  };
  //this.http.get(this.url).subscribe(
    this.http.get(environment.apiurl +'/api/data/json', httpOptions).subscribe(  
    (dronesdt:any)=>{
      localStorage.setItem("dronesdt",JSON.stringify(dronesdt));
      console.log("Drones",JSON.parse(localStorage.getItem("dronesdt")));
 } 
  )
}

So how can I get the latlong array in console. Can anyone please help me regarding this.

4
  • Try console.log(dronesdt.Drone.latlong); Commented Aug 24, 2020 at 5:48
  • Have you tried declaring interfaces to represent your structure and using the generic version of get => this.http.get<Drone>(...)? Commented Aug 24, 2020 at 6:29
  • No,I have not involved in this area before can you please help me to represent the structure of the required json object..and my goal is receive the latlong array from the object. Commented Aug 24, 2020 at 6:34
  • There are multiple tutorials/answers to be found regarding this topic, e.g. stackoverflow.com/questions/54625967/… Commented Aug 24, 2020 at 6:49

1 Answer 1

0

You can get the latlong field like this:

dashboard.service.ts

mapping(token){
  let httpOptions = {
    headers: new HttpHeaders({
    'Content-Type': 'application/json',
      'Authorization': 'Token ' + token
    })
  };
  // this.http.get(this.url).subscribe(
    this.http.get(environment.apiurl +'/api/data/json', httpOptions).subscribe(  
    (dronesdt:unknown /*Try to avoid any and statically type values as far as possible*/)=>{
      localStorage.setItem("dronesdt",JSON.stringify(dronesdt));
      console.log("Drones", dronesdt.Drone.latlong);
 } 
  // )
}

There is no need to JSON.parse the object in the console since you have already fetched the latest data right in that function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.