【问题标题】:Why don't I get json data in Angular为什么我不能在 Angular 中获取 json 数据
【发布时间】:2020-08-31 15:00:03
【问题描述】:

我在 webapi 中有一个类

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

并在 ASP.NetCore 中有如下方法:

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

在另一边 (appAngular),我在 module.ts 中进行了导入 import {HttpClientModule } from '@angular/common/http'; 这是我的代码服务:

图片

在我的控制器上

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

    })
   }
…
}

如果我在浏览器中调用 api,我会得到这个

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

然后在 Angular 应用程序中,我只得到 {object Object}。当我尝试输入 console.log('respuesta:'+response.json); 这会抛出 undefined

我忘记了一个重要的事情是打字稿中的 mi 类;我有:

import { Icliente } from './ICliente';

export interface Response {
    exito:number;
    mensaje:string;
    data:Icliente[];
}

【问题讨论】:

  • 代替console.log('respuesta:'+response); 你可以试试:`console.log('respuesta:', response);`
  • 你应该得到data:{"exito":1,"mensaje":null,"data":[{"id":1,"nombre":"Redmon Redinton"}]}。我认为: 在第一个数据之后丢失,} 在最后丢失。
  • @Sébastien Temprado 我将我的字符串更新为:{"exito":1,"mensaje":" ","data":[{"id":1,"nombre":"Liliana Torres "},{"id":2,"nombre":"Redmon Redinton"}]} 我在 JSON Validator 中测试它没问题,但我无法获得 mi [数据]

标签: javascript json angular typescript asp.net-core


【解决方案1】:

这是您的 json,位于 response 中:

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

要访问此 json 中的属性,您可以这样做(例如,对于 mensaje):

response.mensaje

获取Liliana Torresresponse.data[0].nombre

response.data[0] 等于:

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

等等。

如果要记录完整的 json,请使用:

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

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

【讨论】:

    【解决方案2】:

    您得到的响应是一个对象。所以为了获取对象的数据,可以这样操作。

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

    如果这能解决您的问题,请告诉我。谢谢。

    【讨论】:

    • 感谢@Parween Kelappan,但我输入:´console.log('respuesta:'+respons['exito']); console.log('resp develop:'+respons['data']);' 但是它的抛出:respuesta:1 resp develop:[object Object],[object Object],[object Object]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多