首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以html-table格式打印JSON格式的JSON-RPC输出

如何以html-table格式打印JSON格式的JSON-RPC输出
EN

Stack Overflow用户
提问于 2020-03-03 19:31:54
回答 1查看 276关注 0票数 0

基本上,我有一个包含客户端和服务器的设置。客户端是在angularjs中实现的,它将get请求发送到服务器,服务器连接到mongoDB并检索结果,并以json格式发送回响应。

我发送的请求如下:

代码语言:javascript
复制
const data = {
  "jsonrpc" : "2.0",
  "method" : "faultall",
  "id" : "1",

  "params": {'argument' : 'something' }
}
this.http.post('http://localhost:80/jsonrpc',data).subscribe( res => console.log(this.json =res.text()))

而我得到的回应是:

代码语言:javascript
复制
{"result": [["water", "001"], ["water", "002"], ["temp", "003"]], "id": "1", "jsonrpc": "2.0"}

现在,我希望将响应打印为html页面中的表格,如下图所示:

EN

回答 1

Stack Overflow用户

发布于 2020-03-04 00:48:10

也许,这段代码会有用。

代码语言:javascript
复制
class AppController {
  
  constructor($q) {
    this.deferred = $q.defer();
  }
  
  getData() {
    const response = {"result": [["water", "001"], ["water", "002"], ["temp", "003"]], "id": "1", "jsonrpc": "2.0"};
    setTimeout(() => {
      this.deferred.resolve(response.result);
    }, 1000);
    return this.deferred.promise;
  }
  
  fillTable() {
    this.dataLoading = true;
    this.getData()
    .then(data => {
      this.data = data;
      this.dataLoading = false;
    });
  }
}

angular.module('app', [])
.controller('appController', AppController);

AppController.$inject = ['$q'];

angular.bootstrap(
  document.getElementById('root'),
  ['app']
);
代码语言:javascript
复制
html, body {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<div id="root" ng-controller="appController as ctrl">
  <div style="margin: 1rem;">
    <button 
      class="btn btn-primary"
      ng-click="ctrl.fillTable()"
    >
      <span ng-bind="ctrl.dataLoading ? 'Loading' : 'Fetch data'"></span>
    </button>
  </div>
  <table class="table" ng-show="ctrl.data.length > 0">
    <thead>
      <tr>
        <th>Name</th>
        <th>Id</th>
      </tr>
    </thead>
      <tr ng-repeat="row in ctrl.data">
        <td ng-bind="::row[0]"></td>
        <td ng-bind="::row[1]"></td>
      </tr>
    <tbody>
    </tbody>
  </table>
  <pre>{{ $ctrl.data | json}}</pre>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60506319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档