Html 将对象转换为json
如何将对象转换为JSON并在html页面上输出Html 将对象转换为json,html,json,angular,Html,Json,Angular,如何将对象转换为JSON并在html页面上输出 public User:UserInfo= { firstName: "O", lastName: "K", email: "ol@op.com", country: "uk", avatarUrl: null, receiveNotifications: true } 您可以尝试以下操作: let jsonStr=JSON.stringify(user). 我可以看出您来自jQuery世
public User:UserInfo=
{
firstName: "O",
lastName: "K",
email: "ol@op.com",
country: "uk",
avatarUrl: null,
receiveNotifications: true
}
您可以尝试以下操作:
let jsonStr=JSON.stringify(user).
我可以看出您来自jQuery世界,但是使用angular.js,事情变得简单多了,请检查以下JSFIDLE: 使用angular.js,您可以附加事件,这要简单得多:
<input type="button" ng-click="showJson()" value="Object To JSON" />
and then in your controller:
$scope.showJson = function() {
$scope.json = angular.toJson($scope.user);
}
我希望你能在你的问题中给我们更多的细节,但我会尽我最大的努力,所以请容忍我!假设你有一个像这样的物体
a.component.ts
public obj = {
name: "Oleg",
question: "convert object to json",
description: "Some cool question about angular and JSON"
}
要显示此数据,您的视图如下所示
a.component.html
<h1> {{ obj.name }} </h1>
<h2> {{ obj.question }} </h2>
<p> {{ obj.description }} </p>
{{obj.name}
{{obj.question}}
{{obj.description}}
请注意我的类成员是如何设置为public的,无论何时创建应用程序的构建,这一点都很重要
如果您在理解上有困难,请参阅我的简化示例。如果你不关心{{User | json}}的格式应该如何,请查看此,否则请提供更多详细信息,please@oleg这是哪个版本的angular?@Abdel Angular2/4 OP是AngularJS而不是AngularJS
<h1> {{ obj.name }} </h1>
<h2> {{ obj.question }} </h2>
<p> {{ obj.description }} </p>