2017-09-05 3 views
-1

Ich benutze angular2, um Daten vom Knoten api abzurufen. Knoten api gibt Daten in einem Format wie dieJSON-Daten in angular2 teilen

{"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}} 

Ich mag nur Wert von low in angular2 Frontend angezeigt werden, aber Problem ist, ich bin nicht in der Lage, dass json Daten in Schlüssel-Wert-Paare zu teilen. Entweder zeigt angular2 ganze Daten oder nichts an. Das ist mein Komponentencode ist die

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"]; 
     }); 
    } 


} 

Daten im JSON-Format empfängt Und das ist mein HTML-Code, der Anzeigedaten

<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> 

Jeder Trick oder Lösung, die JSON-Daten in die Schlüsselwertpaare aufgeteilt?

+0

Bitte zeigen Sie den Code für 'this.AppService.getblockchaininfo()' ein JSON-String in ein Objekt konvertieren. –

+0

Was gibt 'console.log (Daten)' zurück? Was ist das? Daten ["_ Körper"] '?? – 12seconds

Antwort

0

Ich bin nicht sicher über die Besonderheiten in Bezug auf Angular2, aber das wird nur mit Plain-Vanilla-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; 
Verwandte Themen