Reading json file from local (for Android / iOS)

Hi, my ionic application needs to read json data from local file. I’m using $http.get method and works just fine in 127.0.0.1 (as web app – using ionic start), but when running on actual ANDROID device, it returns 404.

$http.get('js/data/someData.json').success(function(response){ //do something with response });

Seems like the path is different for android. I did some search on google, but couldn’t find a good solution. Any suggestions?

Thanks!

1 Like

did you solve this? I have the same problem

Yeah. You need different path for android.
Here…

var url = "";
if(ionic.Platform.isAndroid()){
    url = "/android_asset/www/";
}
5 Likes

This also worked for me, thanks a ton!

1 Like

would you share your all source code of read file json with codepan?
this my first time using angularjs and ionic.

thanks in advance. :blush:

1 Like

Just search in github, you’ll find tons…

what ‘search key’ i have to use?
i didn’t find one of it… Sorry :person_frowning:

if i’m going to use this code “$http”, what exactly i have to do first?
or just write the code in controller/module?
or maybe anyelse i have to do if im going to use the code?

thanks is advance… :smile:

Not sure what you are looking for, but $http code is in my question.

cool, works for me!!!

I don’t seem to have any issues with loading a JSON file from an Android device or from the Ionic View app on Android. Here’s the code I use at the top of a controller. My JSON file is in an appdata folder directly under the www folder. For example, the file is in www/appdata/file.json.

Give this a try and see if it works for you.

.controller('loadController', function ($scope, $http) {
    $http.get('appdata/file.json')
        .success(function (data) {
            // The json data will now be in scope.
            $scope.myJsonData = data;
        });
});

Hope that helps,
~ Brad

What is the url for iOS?

1 Like

Tried this but it’s not working.

2 Likes

Hi,

I had a similar issue
using $http.get(’/data/file.json’) - did not work because it was not understood as a relative path.

changing it to ‘data/file.json’ without the 1st forward slash - fixed the issue for me.

JW.

Hi,

And for iOS , anybody knows ?

Regards

Hi, check my answer above.

var url = "";
if(ionic.Platform.isAndroid()){
	url = "/android_asset/www/";
}

then use $http

		$http
			.get(url+'js/data/filename.json')
			.success(function(response){ // do something });

so for iOS, url should be just pointing from /www/ folder…
in my case i had /www/js/data/filename.json,
so for iOS, i used /js/data/filename.json directly

2 Likes

yap. you should not have slash in front.

Thank you very much ozexpert !

Problem solved for iOS too !

1 Like

Thanks ozexpert is OK for me!

1 Like

Thanks a lot @ozexpert… Just a bit curious, why did we go for $http.get() service for fetching a json stored locally? Is there another way we can get the json (I tried using angular.fromJson() but it requires to have a ready json object, we can’t pass in a file path)? Pardon me, just starting with ionic and angular.

Thanks! works for me!