从assets文件夹中的json文件中获取数据 [英] fetching data from json file in assets folder

查看:1042
本文介绍了从assets文件夹中的json文件中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从离子2应用程序中的json文件中获取数据,我在构造函数中使用下面的代码从资源文件夹中获取json,但是获取错误提供的参数与调用目标的任何签名都不匹配。我缺少什么,请建议。



  this.http.get('assets / sample.json')。 map((res)=> res.json()。records).subscribe(data => {this.data = data; console.log(json data+ data);},(rej)=> {console.error(无法加载本地数据,rej)});  

解决方案

所以我解决了它,来自assets文件夹的json文件的路径如下所示,帮助我获得在设备上显示数据的答案。



<预类= 片段码-JS郎-JS prettyprint-越权> this.http.get( '/ android_asset /网络/资产/数据/ subjects.json') .map(res => res.json())。subscribe(response => {this.posts = response; console.log(this.pos) ts);},} err => {console.log(哎呀!); });



如果我们使用 / assets / data / subjects.json ,这将在浏览器上显示数据,但我们不能使用能够在浏览器和设备上获取数据的单一路径,因为它们都具有不同的文件系统,所以我们需要不同的方式来访问设备上的数据。


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

解决方案

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.

这篇关于从assets文件夹中的json文件中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆