I'm having trouble accessing keys and values in my json file. I've tried a bunch of things but my focus is shot this week and I'm stuck.
Here's my transport.json file..
{"transportation":[
{"Springfield":[{
"bus":[{
"start": 6,
"end": 24,
"stops":["oak street", "main street"]
}],
"taxi":[{
"start": 25,
"end": 25,
"stops":["all"]
}]
}]},
{"Pleasantville":[{
"bus":[{
"start": 6,
"end": 22,
"stops":["centre street", "river street"]
}],
"taxi":[{
"start": 25,
"end": 25,
"stops":["all"]
}],
"train":[{
"start": 6,
"end": 23,
"stops":["uptown", "downtown"]
}]
}]}
]}
The two things I'm trying to do are..
I want to be able to alert the bus
start
value in users current area.I want to loop through the bus
stops
to compare to users current stop.
Here's my js code..
var currentArea = 'Pleasantville'; // this variable changes
var currentStop = 'uptown'; // this variable changes
$.getJSON("transport.json", function(jsontransportation) {
$(jsontransportation.transportation).each(function(dataaaa) {
var areaName = Object.keys(this);
if (areaName == currentArea) { // this works to find correct area in json
$(this.bus).each(function(key, value) { // i can't get this loop to work
alert(this.start); // alerts nothing, no errors
$(this.stops).each(function(key) { // now im trying to loop through keys in 'stops'
if (this.key === currentStop) { // to compare stop to current area
alert('the bus stops here at ' + currentStop); // and alert if there is a stop here
} else {
alert('the bus does not stop here at ' + currentStop); // else alert no stop here
}
})
});
}
});
});