Reputation: 1295
I am trying to work with a json data set that doesnt use numbers as keys but strings
this makes it a bit more complicated to return results.
so if the json structure is something like this
{
"total": 1,
"data": {
"tab": {
"what-you-doing": {
"tabColWidth": "2",
"tabColHeight": "2",
"category": "CategoryA",
},
"tab-smaller-78e": {
"tabColWidth": "1",
"tabColHeight": "1",
"category": "CategoryB",
}
}
}
}
and i then want to query this
my js to getJson
function getJSON (name, path) {
var promise = $.ajax({
url: path + name + '.json',
method: 'get',
dataType: 'json'
});
return promise;
}
and then working with the data set
$(document).ready(function(){
var promise = getJSON('feed', "../data/");
$.when(promise).then(function(result) {
$(result.data.tab).each(function(index, element) {
console.log(element);
console.log(element['what-you-doing'].category);
});
});
obviously I dont want to have to specify what-you-doing in the code so how can I get this name within this loop and also look at the results underneath it.
the reason this is no using [] within the json is to make it easier to find this result and not have to do another loop.
});
Upvotes: 0
Views: 70
Reputation: 2269
Are you asking how to loop over the keys in an object in javascript? Because the JSON gets converted into a javascript object... You probably just need
for (var key in element) {
console.log(key);
console.log(element[key]);
}
Upvotes: 2