1

I have a class in a webapi

public class Respuesta
{
    public int Exito { get; set; }
    public string Mensaje { get; set; }
    public object Data { get; set; }
}

and have the following method in ASP.NetCore:

public IActionResult Get()
{ ..
using (NegociosAPIContext db = new NegociosAPIContext())
                {
                    var lst = db.Cliente.ToList();
                    oRespuesta.Exito = 1;
                    oRespuesta.Data = lst;
                    return Ok(oRespuesta);
                }

In the otherside (appAngular), I did the import in module.ts import {HttpClientModule } from '@angular/common/http'; and it´s my code service:

Image

enter image description here

and on my controller

import { Component, OnInit } from '@angular/core';
import {ApiclienteService} from '../service/apicliente.service';

@Component({
  selector: 'app-cliente',
...
})
export class ClienteComponent implements OnInit {

  constructor(private apiCliente: ApiclienteService) {
    apiCliente.getClientes().subscribe(response => {
      console.log('respuesta:'+response);

    })
   }
…
}

If I make a call to the api in the browser, I get this

{
    "exito":1, 
    "mensaje":" ",
    "data":[
        {
            "id":1, 
            "nombre":"Liliana  Torres"
        },
        {
            "id":2,
            "nombre":"Redmon Redinton"
        }
    ]
}

Then in Angular app, I get only the {object Object}. When I try to put console.log('respuesta:'+response.json); this throws undefined

An important that i forget was mi class in typescript; i have:

import { Icliente } from './ICliente';

export interface Response {
    exito:number;
    mensaje:string;
    data:Icliente[];
}
3
  • 1
    instead of console.log('respuesta:'+response); can you try: ` console.log('respuesta:', response);` Commented May 14, 2020 at 22:16
  • You should have get data:{"exito":1,"mensaje":null,"data":[{"id":1,"nombre":"Redmon Redinton"}]}. I think : is missing after the first data and a } is missing at the end. Commented May 15, 2020 at 12:23
  • @Sébastien Temprado i update my string to : {"exito":1,"mensaje":" ","data":[{"id":1,"nombre":"Liliana Torres"},{"id":2,"nombre":"Redmon Redinton"}]} and i test in JSON Validator its ok, but i can´t obtaint mi [data] Commented May 19, 2020 at 15:21

2 Answers 2

1

Here is your json which is in response :

{
    "exito":1, 
    "mensaje":" ",
    "data":[
        {
            "id":1, 
            "nombre":"Liliana  Torres"
        },
        {
            "id":2,
            "nombre":"Redmon Redinton"
        }
    ]
}

To access to an attribute in this json, you can do (for example, for mensaje):

response.mensaje

To get Liliana Torres : response.data[0].nombre.

response.data[0] is equal to :

{
    "id":1, 
    "nombre":"Liliana  Torres"
}

and so on.

If you want to log the full json, use:

console.log('respuesta:', response);

or

console.log('respuesta:' + JSON.stringify(response));

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

Comments

0

The response you are getting is an object. So in order to get the data of the object, you can do it like this.

    console.log('respuesta:' response['data']);

Let me know if this solves your issue. Thank you.

1 Comment

thanks @Parween Kelappan, but i put: ´console.log('respuesta:'+respons['exito']); console.log('resp develop:'+respons['data']);´ however its throw: respuesta:1 resp develop:[object Object],[object Object],[object Object]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.