2017-09-05 57 views
-1

我正在使用angular2從節點API中獲取數據。節點API返回數據的格式一樣,在angular2中分割json數據

{"height":{"low":4,"high":0,"unsigned":true},"currentBlockHash":{"buffer":{"type":"Buffer","data":[8,4,18,32,197,125,99,165,77,70,46,19,215,237,19,57,219,242,168,130,134,153,184,56,68,211,255,62,122,245,216,154,192,254,179,139,26,32,169,218,156,201,73,252,127,198,103,103,43,100,81,90,113,109,163,137,159,156,140,148,125,28,104,79,145,218,72,145,206,6]},"offset":4,"markedOffset":-1,"limit":36,"littleEndian":true,"noAssert":false},"previousBlockHash":{"buffer":{"type":"Buffer","data":[8,4,18,32,197,125,99,165,77,70,46,19,215,237,19,57,219,242,168,130,134,153,184,56,68,211,255,62,122,245,216,154,192,254,179,139,26,32,169,218,156,201,73,252,127,198,103,103,43,100,81,90,113,109,163,137,159,156,140,148,125,28,104,79,145,218,72,145,206,6]},"offset":38,"markedOffset":-1,"limit":70,"littleEndian":true,"noAssert":false}} 

我只想在angular2前端顯示的low價值,但問題是我無法說JSON數據拆分成鍵 - 值對。 angular2顯示整個數據或不顯示任何內容。 這是JSON格式接收數據我的分量代碼

import {Component,OnInit} from '@angular/core'; 
import { AppService } from '~/./../app/app.service'; 

@Component({ 
    selector: 'dashboard', 
    styleUrls: ['./dashboard.scss'], 
    templateUrl: './dashboard.html' 
}) 
export class Dashboard implements OnInit { 

    constructor(private AppService: AppService) { 
    } 

    ngOnInit(){ 
     this.getStats(); 
    } 

    BlockchainHeight:any; 

    getStats(){ 
     this.AppService.getblockchaininfo().subscribe(data=>{ 
      console.log(data); 
      this.BlockchainHeight=data["_body"]; 
     }); 
    } 


} 

這是我的HTML代碼顯示數據

<div class="row"> 
    <div class="col-xl-4 col-lg-4 col-md-4 col-sm-4 col-4"> 
    <ba-card> 
    <h3> Blockchain Height</h3> 
    {{BlockchainHeight}} 
    </ba-card> 
    </div> 

任何特技或溶液以拆分JSON數據轉換成密鑰值對?

+0

請顯示'this.AppService.getblockchaininfo()的代碼'。 – 2017-09-05 19:52:20

+0

'console.log(data)'返回什麼?這是什麼'data [「_ body」]'? – 12seconds

回答

0

林不知道關於Angular2的細節,但這將JSON字符串轉換爲對象,具有隻是普通的香草的JavaScript

var jsonResponse = "{"height":{"low":4,"high":0,"unsigned":true},"currentBlockHash":{"buffer":{"type":"Buffer","data":[8,4,18,32,197,125,99,165,77,70,46,19,215,237,19,57,219,242,168,130,134,153,184,56,68,211,255,62,122,245,216,154,192,254,179,139,26,32,169,218,156,201,73,252,127,198,103,103,43,100,81,90,113,109,163,137,159,156,140,148,125,28,104,79,145,218,72,145,206,6]},"offset":4,"markedOffset":-1,"limit":36,"littleEndian":true,"noAssert":false},"previousBlockHash":{"buffer":{"type":"Buffer","data":[8,4,18,32,197,125,99,165,77,70,46,19,215,237,19,57,219,242,168,130,134,153,184,56,68,211,255,62,122,245,216,154,192,254,179,139,26,32,169,218,156,201,73,252,127,198,103,103,43,100,81,90,113,109,163,137,159,156,140,148,125,28,104,79,145,218,72,145,206,6]},"offset":38,"markedOffset":-1,"limit":70,"littleEndian":true,"noAssert":false}} "; 
var decodedJsonObject = JSON.parse(jsonResponse); 
var low = decodedJsonObject.height.low;