I have tried everything to assign my JSON data (JSON Object?) with $http.get("https://server url to my json data").success(function (response) {} to a local variable in javascript, but it doesnt't work. I am really confused with all of this json strings and objects. Here's my code.
Form of my json data which I get from the server
{
"status":"success",
"data":{
"user":{
"username":"user1",
"fullName":"name "
},
"vehicles":[ ],
"chargeBoxes":
[
{
"id":1,
"name":"Station 1",
"availability":"offline",
},
{
"id":2,
"name":"Station 2",
"availability":"online",
},
{
"id":3,
"name":"Station 3",
"availability":"online",
}
]
}
So, my question is how I can use this data an store it in an js array. I need it for my javascript highchart controller. I tried this..
controller.js
myApp = angular.module('ServiceDashboard');
myApp.controller('DataController' function($scope, $http, $interval) {
$http.get("https://server-url.com/../login?username=..&password=..").success(function(response) {
$scope.jsonData = response.data;
});
var categorieData = [];
var json = JSON.parse(jsonData);
for (var i = 0; i <= jsonData.chargeBoxes.length - 1; i++)
{
categorieData[i] = jsonData.chargeBoxes[i].name;
}
//This works really fine when I do it this way (test case) (json variable in just one line and with ' '
// Here I have a json string (one line with ' ')
var jsonData= '{"status": "success", "data": {"chargeboxes":[{..},{..},{..}]}';
// and then I parse this json string to an json object and iterate over it and store the chargeBoxes.name values into the categorieDate array.
But when I try it with the real form of my json data (multiline with {}), then it doesn't work.
var jsonData = {
"status":"success",
"data":{
"user":{
"username":"user1",
"fullName":"name "
},
"vehicles":[ ],
"chargeBoxes":
[
{
"id":1,
"name":"Station 1",
"availability":"offline",
},
{
"id":2,
"name":"Station 2",
"availability":"online",
},
{
"id":3,
"name":"Station 3",
"availability":"online",
}
]
};
I really don't know what to do anymore. First of all, I would like to try it with a local variable like above (var jsonData = {..}; because with oneline string ' ' it works), and then I would like to use this json data directly from the server ($scope.jsondata...).
Thanks!