Search code examples
jsonionic-frameworkandroid-assets

fetching data from json file in assets folder


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


Solution

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