在离子应用程序中读取json文件的内容

问题描述 投票:1回答:1

我正在尝试构建一个带有离子的应用程序,该应用程序从本地`.json'文件读取数据并使用此数据填充页面。但我已经在努力将文件导入页面。我现在拥有的是:

import { Component } from "@angular/core";

interface Entry {
    name:           string,
    telephone:      string
}

interface EntryList {
    entryList:  Array<Entry>;
}

@Component({
    selector:    'page-list',
    templateUrl: 'list.html'
})
export class ListPage {

    entryList: EntryList;

    constructor() {
        this.load_entries();
    };

    load_entries () {
        this.entryList = JSON.parse(
            // ?
        )
    };
}

.json文件包含以下条目:

[
{"name": "Person A","telephone": "1234"},
{"name": "Person B","telephone": "12345"}
]

我不知道怎么从这里开始。将数据导入应用程序的正确方法是什么?

json angular cordova parsing ionic-framework
1个回答
1
投票

请试试这个:

constructor(public http: HttpClient) {
    this.load_entries();
};

load_entries(filePath: string) {  //filePath: 'assets/test.json'

    this.http
        .get(filePath)
        .subscribe((data) => {
           console.log(data);
        });
}

当然,你必须先导入HttpClient

import { HttpClient } from '@angular/common/http';
© www.soinside.com 2019 - 2024. All rights reserved.