fetching data from json file in assets folder

2019-07-27 06:52发布

Trying to fetch data from a json file in ionic 2 application, I am using the below piece of code within constructor for fetching json from assets folder, but getting error "Supplied parameters do not match any signature of call target." What i am missing, Please suggest.

this.http.get('assets/sample.json')
.map((res) => res.json().records)
.subscribe(data => {
this.data = data;
console.log("json data"+ data);
}, (rej) => {console.error("Could not load local data",rej)});

2条回答
【Aperson】
2楼-- · 2019-07-27 07:18

So I solved it, the path of json file from assets folder used as below help me to get the answer that displayed the data on device.

 this.http.get('/android_asset/www/assets/data/subjects.json').map(res => res.json()).subscribe(
    response => {
        this.posts = response;
		console.log(this.posts);
    },
    err => {
        console.log("Oops!");
    }); 

While if we use /assets/data/subjects.json, this will display the data on browser, though we can not use a single path that could be able to get the data on both browser as well as on device, because both have the different file system so we need different way to access the data on the devices.

查看更多
叼着烟拽天下
3楼-- · 2019-07-27 07:36
 this.http.get("assets/sample.json")
.subscribe(res =>{
    this.data=res.json();
    this.data=Array.of(this.data);
    console.log(this.data);
}, error =>{
    console.log(error);

});

Try this.

查看更多
登录 后发表回答