【问题标题】:How to access the Json data from a service without data model in Angular如何在 Angular 中从没有数据模型的服务访问 Json 数据
【发布时间】:2017-10-09 09:58:36
【问题描述】:

我有这个嵌套的 json,我从我的服务获取调用

{
    "id": "7979d0c78074638bbdf739ffdf285c7e1c74a691",
    "seatbid": [{
        "bid": [{
            "id": "C1X1486125445685",
            "impid": "12345",
            "price": 0,
            "adm": {
                "native": {
                    "link": {
                        "url": "http: //i.am.a/URL"
                    },
                    "assets": [{
                        "id": "001",
                        "required": 1,
                        "title": {
                            "text": "Learn about this awesome thing"
                        }
                    }],
                    "imptrackers": ["https://trks.c1exchange.com/trk/c?pid=123&et=i&profileid=123&siteid=12345&adslot=AUM&adsize=300x250&imprtype=IMAGE&cpm=0.0&dspcid=834&rndid=368997882&adomain=[c1exchange.com]&urid=8787893965303118897&uimprid=2063341746607836879&urespid=0"]
                }
            },
            "adomain": ["c1exchange.com"],
            "cid": "834"
        }]
    }],
    "cur": "USD"
}{
    "id": "7979d0c78074638bbdf739ffdf285c7e1c74a691",
    "seatbid": [{
        "bid": [{
            "id": "C1X1486125445685",
            "impid": "12345",
            "price": 0,
            "adm": {
                "native": {
                    "link": {
                        "url": "http: //i.am.a/URL"
                    },
                    "assets": [{
                        "id": "001",
                        "required": 1,
                        "title": {
                            "text": "Learn about this awesome thing"
                        }
                    }],
                    "imptrackers": ["https://trks.c1exchange.com/trk/c?pid=123&et=i&profileid=123&siteid=12345&adslot=AUM&adsize=300x250&imprtype=IMAGE&cpm=0.0&dspcid=834&rndid=368997882&adomain=[c1exchange.com]&urid=8787893965303118897&uimprid=2063341746607836879&urespid=0"]
                }
            },
            "adomain": ["c1exchange.com"],
            "cid": "834"
        }]
    }],
    "cur": "USD"
}

问题是我想在我的组件中使用它而没有相同的数据模型

目前在我的组件中

 ngOnInit() {

    this.weatherService.getPocData().subscribe( res =>  {
      this.openRtbResponse = res.openRtbResponse;

    });

  }

在我的模板中

 {{openRtbResponse | json}}

我可以访问相同的。

但是现在当我想像这样访问嵌套值时,它给出了 undefined like

this.openRtbResponse.seatbid[0].bid[0].adm.native.assets[0].id 

它会抛出错误。

没有数据模型我不能使用json值

【问题讨论】:

标签: angular


【解决方案1】:

Typescript 是 Javascript 的超集,因此任何未明确键入的内容都是 any 类型——这本质上意味着它是您的“传统”Javascript 对象。

您应该能够在没有支持类型的情况下访问这些属性。我猜您尝试访问它太早或路径错误,这给了您错误。

直接回答您的问题:是的,您应该可以访问它。

【讨论】:

  • 是的,这就是我的感受,但我正在使用相同的服务,例如:http get 调用导致了问题
  • 我只是调试/记录你的方式。我想这要么是路径问题,要么是时间问题。
【解决方案2】:

您必须在这样的请求响应中调用 .json() 以获得 JSON 对象:

function getPocData() {
    return http.get('friends.json')
               .map(response => response.json());
}

请务必在 weatherServicegetPocData() 函数中执行此操作。

【讨论】:

  • 好的,所以也许你需要使用 elvis 运算符:this.openRtbResponse?.seatbid[0]?.bid[0]?.adm?.native?.assets[0]?.id,它告诉 Angular 不检查 ? 之后的内容,如果之前的值为 null/undefined
猜你喜欢
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2016-05-28
  • 2020-06-06
  • 2022-09-21
  • 2015-03-24
  • 2020-12-26
相关资源
最近更新 更多