I am trying to parse the below json.If I try to get the value as in the below code is showing undefined.
Is there is any way I can parse the json in which key as the space?
{
"Trasport Fee":{
"KARNATAKA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":4000
}
}
},
"ANDRA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":100
}
}
},
"MP":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":600
}
}
},
"MAHARASHTRA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":600
}
}
}
}
}
var promise=feeService.getTraspotaionFee();
promise.then(function(data){
$scope.charge=data;
console.log($scope.charge.data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["amount"]);
});
{
"Trasport Fee":{
"KARNATAKA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":4000
}
}
},
"ANDRA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":100
}
}
},
"MP":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":600
}
}
},
"MAHARASHTRA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":600
}
}
}
}
var promise=feeService.getTraspotaionFee();
promise.then(function(data){
$scope.charge=data;
console.log($scope.charge.data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]);
});
your key is transport fee not transforation.You missed ALL_PLACES before amount
Your JSON is not valid a curly bracket is missing at the end. Use a JSON Validator, thats what I do first if there are problems with "JSON" data.
https://jsonformatter.curiousconcept.com/ <-- nice validator
var promise=feeService.getTraspotaionFee();
promise.then(function(data){
$scope.charge=data;
//changes here
console.log($scope.charge.data[Object.keys($scope.charge.data)[0]]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]);
});
Change above code and use
I think it should be:
console.log($scope.charge.data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]);
^^^^^^^^^^^^^^
I also fixed your JSON (missing brackets):
{
"Trasport Fee":{
"KARNATAKA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":4000
}
}
},
"ANDRA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":100
}
}
},
"MP":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":600
}
}
},
"MAHARASHTRA":{
"ALL_DISTRICT":{
"ALL_PLACES":{
"amount":600
}
}
}
}
}
observation :
Issue is not with the spaces in key. you are accessing the value in correct way only.
You got undefined because you are trying to accessing the amount
property inside ALL_DISTRICT
object which is not available.
Try this :
$scope.charge.data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]
DEMO
var data = { "Trasport Fee": { "KARNATAKA": { "ALL_DISTRICT": { "ALL_PLACES": { "amount": 4000 } } }, "ANDRA": { "ALL_DISTRICT": { "ALL_PLACES": { "amount": 100 } } }, "MP": { "ALL_DISTRICT": { "ALL_PLACES": { "amount": 600 } } }, "MAHARASHTRA": { "ALL_DISTRICT": { "ALL_PLACES": { "amount": 600 } } } } }; console.log(data["Trasport Fee"]["KARNATAKA"]["ALL_DISTRICT"]["ALL_PLACES"]["amount"]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.