Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
Hi all,
I have a .json file that I will call from home.js script.
I uploaded the .json file into the Vuforia resources: how can I include this .json file, so that I can access it?
I've tried many functions described in similar topics (here or here ) and functions I found browsing the internet ($scope.getJSON('...'), $scope.loadJSON('...')...) but none of them worked.
Best regards,
Stefano
Solved! Go to Solution.
Hi @Stefano,
here a simple example which shows how to load a json file form resource/Upload project folder and saved it to a global/$scope variable:
//=======================================================
//Global Variables
var pjson ="motordata.json";
//JSON File Name with extension
$scope.jsonData={}; //global $scope variable where the received json file is saved
gotJSON=function(data){
try{
$scope.jsonData=JSON.parse(data);
console.warn( JSON.stringify($scope.jsonData))
}
catch(wrong){ console.warn(wrong);}
}
//=======================================================
doRead=function (jsonFile){
fetch(jsonFile)
.then(response=>response.text())
.then(data=>gotJSON(data))
}
//=======================================================
$scope.Init=function() {
doRead('app/resources/Uploaded/'+pjson);
//read the json file form the resources/Upload Vuforia Studio Project folder
// ... do here other init stuff
//....
}
//=======================================================
$scope.$on('$ionicView.afterEnter', function() {
console.info("started $ionicView.afterEnter at time: "+Date(Date.now()))
$scope.Init();
});
//execute the init function when the view was loaded
//=======================================================
//....
In the example above I used the fetch method but also $http service could be used: example:
//========================================================================
$http.get('app/resources/Uploaded/' + jsonFile).success(function(data, status, headers, config) {
$scope.myJsonData=data;
// here you could work with myJsonData e.g.
console.log(JSON.stringify($scope.myJsonData) // e.g. print it to console
// depending on the content of the json file you can do some actions
try {
Object.keys($scope.myJsonData).forEach(function(item){
console.log ("Text =" +$scope.myJsonData[item].Text);
var labelID = $scope.myJsonData[item].LabelID;
$scope.view.wdg[labelID].text = $scope.myJsonData[item].Text;
$scope.view.wdg[labelID].visible = true;
$scope.view.wdg[labelID].x = $scope.myJsonData[item].x;
$scope.view.wdg[labelID].y = $scope.myJsonData[item].y;
$scope.view.wdg[labelID].z = $scope.myJsonData[item].z;
$scope.view.wdg[labelID].fontColor = $scope.myJsonData[item].status;
});
} catch(ex) {
console.log ("Possible Error: When trying to work through Grid data passed from IOT service there was an issue Exception:"+ex);
}
// this example above work only for json array which contains ojects with key/ property pairs
// Text: ... , x: .... , y: ... z: .... , LabelID : ...
});
Hi @Stefano,
here a simple example which shows how to load a json file form resource/Upload project folder and saved it to a global/$scope variable:
//=======================================================
//Global Variables
var pjson ="motordata.json";
//JSON File Name with extension
$scope.jsonData={}; //global $scope variable where the received json file is saved
gotJSON=function(data){
try{
$scope.jsonData=JSON.parse(data);
console.warn( JSON.stringify($scope.jsonData))
}
catch(wrong){ console.warn(wrong);}
}
//=======================================================
doRead=function (jsonFile){
fetch(jsonFile)
.then(response=>response.text())
.then(data=>gotJSON(data))
}
//=======================================================
$scope.Init=function() {
doRead('app/resources/Uploaded/'+pjson);
//read the json file form the resources/Upload Vuforia Studio Project folder
// ... do here other init stuff
//....
}
//=======================================================
$scope.$on('$ionicView.afterEnter', function() {
console.info("started $ionicView.afterEnter at time: "+Date(Date.now()))
$scope.Init();
});
//execute the init function when the view was loaded
//=======================================================
//....
In the example above I used the fetch method but also $http service could be used: example:
//========================================================================
$http.get('app/resources/Uploaded/' + jsonFile).success(function(data, status, headers, config) {
$scope.myJsonData=data;
// here you could work with myJsonData e.g.
console.log(JSON.stringify($scope.myJsonData) // e.g. print it to console
// depending on the content of the json file you can do some actions
try {
Object.keys($scope.myJsonData).forEach(function(item){
console.log ("Text =" +$scope.myJsonData[item].Text);
var labelID = $scope.myJsonData[item].LabelID;
$scope.view.wdg[labelID].text = $scope.myJsonData[item].Text;
$scope.view.wdg[labelID].visible = true;
$scope.view.wdg[labelID].x = $scope.myJsonData[item].x;
$scope.view.wdg[labelID].y = $scope.myJsonData[item].y;
$scope.view.wdg[labelID].z = $scope.myJsonData[item].z;
$scope.view.wdg[labelID].fontColor = $scope.myJsonData[item].status;
});
} catch(ex) {
console.log ("Possible Error: When trying to work through Grid data passed from IOT service there was an issue Exception:"+ex);
}
// this example above work only for json array which contains ojects with key/ property pairs
// Text: ... , x: .... , y: ... z: .... , LabelID : ...
});