Reading json file from local (for Android / iOS)


#1

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!


#2

did you solve this? I have the same problem


#3

Yeah. You need different path for android.
Here…

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

#4

This also worked for me, thanks a ton!


#5

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:


#6

Just search in github, you’ll find tons…


#7

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:


#8

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


#9

cool, works for me!!!


#10

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


#12

What is the url for iOS?


#13

Tried this but it’s not working.


#14

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.


#15

Hi,

And for iOS , anybody knows ?

Regards


#16

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


#17

yap. you should not have slash in front.


#18

Thank you very much ozexpert !

Problem solved for iOS too !


#19

Thanks ozexpert is OK for me!


#20

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.


#21

Thanks! works for me!