简体   繁体   English

Angular2 / Typescript:在组件中渲染具有unknow结构的json对象

[英]Angular2 / Typescript : render json object with unknow structure in component

I need to render a json object that is the output of a webapi call on a component. 我需要呈现一个json对象 ,该对象是组件上webapi调用的输出。

The problem is that this response has not a fixed structure , it can change depending on the webapi execution. 问题在于此响应没有固定的结构 ,可以根据webapi的执行情况进行更改。

Sometimes it can be a simple array, other time an array of object etc... 有时它可以是一个简单的数组,有时它可以是对象的数组等。

How can I render it without knowing the output structure ? 我如何在不知道输出结构的情况下进行渲染?

For who comes from PHP development, I need like something like print_r function... 对于谁来自PHP开发,我需要像print_r函数之类的东西...

Thanks Regards 感谢和问候

It depends on how you want to render it. 这取决于您要如何渲染它。

To debug you can use the json pipe 要调试,您可以使用json管道

{{data | json}}

Otherwise you could check if the returned response is an array or an object and render it based on that. 否则,您可以检查返回的响应是数组还是对象,然后根据该结果进行呈现。

if (Array.isArray(data)) {
  // render as array
} else {
  // render as something else
}

I would probably try to normalize the datastructure that is returned from an angular service so that it's always an array of objects, and then based on if the objects contain different keys render it differently. 我可能会尝试规范化从角度服务返回的数据结构,以使其始终是对象数组,然后根据对象是否包含不同的键来对其进行不同的呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM