如何在前端html页面上呈现JSON
How to render JSON on a frontend html page
我试图在HTML页面上显示JSON字符串。JSON字符串看起来像这样:
{
email: '111@11.com',
username: '1111',
roles: {},
array: ['data','data'] },
{
email: 'go@1.com',
username: 'go',
roles: {},
array: [ 'c', 'c' ] } ]
HTML是这样的:
<div id="my_id">
<h3>json.email1</h3>
<div>
<div class="content">
<p>json.array1</p>
</div>
</div>
<h3>json.email2</h3>
<div>
<div class="content">
<p>json.array2</p>
</div>
</div>
<h3>json.email3</h3>
<div>
<div class="content">
<p>json.array3</p>
</div>
</div>
</div>
必须自动添加HTML以确保所有JSON元素都填充在HTML标记中。最简单的方法是什么?有人能帮我一下吗?
HTML:<div>
<div id="people">
<h3><a href="mailto:{{this.email}}">{{this.name}}</a></h3>
<div>
<div class="content">
<p>This example simply sets a class attribute to the details and let's an
external stylesheet toggle the collapsed state.
</p>
</div>
</div>
<script>
var data = [
{ name: "Olga", age: 20, email: "aaa@example.com" },
{ name: "Peter", age: 30, email: "bbb@example.com" },
{ name: "Ivan", age: 15, email: "ccc@example.com" },
];
var list = $("div#people").repeatable(); // declaring the repeatable
list.value = data; // that's data population, sic!
</script>
</div>
</div>
HTML: <div class="people">
<h3>Foe</h3>
<div>
<div class="content">
<p>This example simply sets a class attribute to the details and let's an
external stylesheet toggle the collapsed state.</p>
</div>
</div>
</div>
上面的代码不显示折叠结构。当整个东西被包装到div class=people中时,它不能正常工作(不显示折叠)。
你可以使用angular.js,它是一个用来做这类事情的库。非常简单:
<html ng-app="myapp">
<body ng-controller="mycontroller">
<div ng-repeat="user in users">
<h3>{{ user.email }}</h3>
<p>{{ user.username }}</p>
<div ng-repeat="data in array">
{{ data }}
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<script>
var app = angular.module('myapp', []);
app.controller = ('mycontroller', function($scope){
$scope.users = {
//your data here
}
});
</script>
<body>
</html>
不久前我设计了所谓的可重复的jQuery插件:https://github.com/c-smile/repeatable
如果你有这样的模板(html片段)
<ul id="people">
<li><a href="mailto:{{this.email}}">{{this.name}}</a> <b if="this.age > 18">18+</b> </li>
<li>No data available</li>
</ul>
和data
var data = [
{ name: "Olga", age: 20, email: "aaa@example.com" },
{ name: "Peter", age: 30, email: "bbb@example.com" },
{ name: "Ivan", age: 15, email: "ccc@example.com" },
];
然后从数据中渲染列表,你将这样做:
var list = $("ul#people").repeatable(); // declaring the repeatable
list.value = data; // that's data population, sic!
相关文章:
- jQuery匹配JSON对象的部分文本
- 在循环中分配json值时,值被覆盖
- 需要帮助设置json数组
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 可以前端maven插件使用节点,npm已经安装
- 使用JS将数组转换为json对象
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用JQuery解析JSON嵌套数组
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 为什么react/redux前端项目在package.json中包含生产依赖项
- 如何在 JSF 中将 JS 函数作为 JSON 属性传递给前端
- Node.js and Express - 将JSON对象从SoundCloud API发送到前端使其成为字符串
- 在前端显示角度 JSON 数组
- 用于前端的后端JSON到HTML
- Django-将JSON数据和HTML一起传递到前端不起作用
- AngularJS(前端)和Laravel(后端)中的JSON/Arrays中为NULL
- 如何在前端html页面上呈现JSON
- 我应该在哪里存储我的JSON Web令牌'non-singlepage'前端应用程序
- 在Node.js中使用AJAX(作为RESTful API)获取JSON文件,并根据单击前端的哪个按钮对其进行解析