2017-09-14 65 views
1

我正在使用JSON API与Angular 4前端,现在我试图显示这个JSON Object的数据。如何拆分角4中的JSON对象4

我用下面的代码:

<div *ngFor="let Questionnaire of struc.data"> 
    <span>{{Questionnaire.attributes.content.headline}}</span><br> 
    <span>{{Questionnaire.attributes.content.text}}</span><br> 
    <span>{{Questionnaire.attributes.content.question}}</span> 
</div> 

<div *ngFor="let Questionnaire of struc.data"> 
    <li>{{Questionnaire.attributes.content.answers}}</li> 
</div> 

它的工作原理,但我得到我的答案格式为:

  • überhauptnicht,Sehr泽尔滕,einige男,häufig,sehrhäufig

而不是看起来像这样:

  • überhauptnicht
  • Sehr泽尔滕
  • einige男
  • häufig
  • sehrhäufig

这有什么错我的代码?

回答

2

你可以做到这一点Questionnaire.attributes.content.answer.split(',')

<div *ngFor="let Questionnaire of struc.data"> 
<div *ngFor="let ans of Questionnaire.attributes.content.answers.split(',')"> 
    <li>{{ans}}</li> 
    </div> 
</div> 

EDIT

您的JSON对象帖子后,似乎答案是一个数组,你需要做到这一点,

<div *ngFor="let Questionnaire of struc.data"> 
    <div *ngFor="let ans of Questionnaire.attributes.content.answers"> 
     <li>{{ans}}</li> 
     </div> 
    </div> 

DEMO

+0

我做了,但我得到了这个错误:无法读取属性'拆分'未定义..我认为它是因为我正在处理数组而不是字符串,因为我的JSON对象,我只是不知道如何改变这个 – betterel

+0

检查更新的答案 – Sajeetharan

+0

真棒!这工作。非常感谢! – betterel