【问题标题】:How to print json array in angular如何以角度打印json数组
【发布时间】:2019-08-26 03:23:57
【问题描述】:

这里是 Stackblitz https://stackblitz.com/edit/angular-parse-object 来自 REST API 的响应格式是

[{"id":123,"name":Test,"value":{"pass": true, "verified": true}}, {"id":435,"name":Test12,"value":{"pass": false, "verified": true}},]
<div *ngFor="let record of student.value">
  <ul *ngFor="let key of objectKeys(record)">
    <li>{{key}} :: {{record[key]}}</li>
  </ul>
</div>

出现错误**无法读取未定义的属性“值”**

<div *ngFor="let rec of student">
      <h2>{{rec.id}}</h2>
         <div *ngFor="let result of rec.value">
               <h4>  {{value.pass}} </h4>
          </div>
   </div>

获取“字符串”类型的错误对象。 NgFor 仅支持绑定到 Iterables,例如 Arrays。

如何解决?

我希望输出为

ID : 123 Name : Test Pass : true [yes] verified : true

【问题讨论】:

  • studentundefined
  • 在 Typescript 和 Console.log(student) 打印中定义 [{"id":123,"name":Test,"value":{"pass": true, "verified": true} }, {"id":435,"name":Test12,"value":{"pass": false, "verified": true}},]
  • 检查typeof student
  • 你需要解析数据
  • this.student = JSON.parse(student)

标签: html typescript angular6


【解决方案1】:

你只需要1个ngFor,你的值不是数组。

<div *ngFor="let rec of student">
      <h2>Id: {{rec.id}} Name: {{rec.name}} Pass: {{rec.value.pass}} Verified: {{rec.value.verified}}</h2>
</div>

参考(可以格式化) https://stackblitz.com/edit/angular-iterator

更新:

像这样用 JSON.parse 改变你的地图函数

this.student = ["{pass: true, verified: true}", "{pass: false, verified: true}"];
    this.student = this.student.map(c=>JSON.parse(c.replace(/([a-zA-Z0-9-]+): ([a-zA-Z0-9-]+)/g, "\"$1\":\"$2\"")));

https://stackblitz.com/edit/angular-parse-object

【讨论】:

  • 抛出错误 SyntaxError: JSON.parse 中位置 32 处的 JSON 中的意外标记 t this.http.get('/api/allreports/' + this.sid, options) .subscribe(result =&gt; { var obj = result.json(); this.student = obj.map(c=&gt;JSON.parse(c.replace(/([a-zA-Z0-9-]+): ([a-zA-Z0-9-]+)/g, "\"$1\":\"$2\""))); });
  • 你检查过这个链接stackblitz.com/edit/angular-parse-object 吗?
  • 你能在这里打印出 var obj 或 result 吗?
  • '[{"id":123,"name":Test,"value":"{pass: true,verified: true}"}, {"id":435,"name" :Test12,"value":"{pass: false, verify: true}"},]'
【解决方案2】:

我只是使用您的 Student Array 编写了一个基本的ngFor 代码。试试这个,希望能帮到你。谢谢

HTML

<div class="studentList" *ngFor="let student of students">
  <ul>
    <li>ID : {{student.id}}</li>
    <li>Name : {{student.name}}</li>
    <li>Pass : {{student.value.pass}} [{{student.value.pass ? 'Yes' : 'No'}}]</li>
    <li>Verified : {{student.value.verified}}</li>
  </ul>
</div>

TS

students = [
{
  "id":123,
  "name":"Test",
  "value":{"pass": true, "verified": true}
}, {
  "id":435,
  "name":"Test12",
  "value":{"pass": false, "verified": true}
  }
]

CSS

.studentList ul {
  list-style: none;
  margin: 0 0 10px;
  padding: 0;
}

.studentList ul li {
  display: inline-block;
  margin-right: 10px;
}

【讨论】:

  • 能否分享链接。谢谢
  • 是的@Hassan 如何将对象作为类型字符串迭代并显示值
  • 关于如何迭代的任何输入
  • 你的学生数组中没有几个额外的引号,你想在你的 .map 方法中做什么?
猜你喜欢
  • 2018-06-09
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多