I'd like to create a function which can cope with JSON in the following nested format. The keys will change ie conditions - could be anything and the subcondition key could be anything but the structure remains the same.
My function is defined as
populatePageEditorMenu: function (jsonPath, topLevel, childLevel){
and called by
populatePageEditorMenu("http://localhost/data/conditions.json", 'conditions', 'subcondition' );
this allows me to point to different bit of json with different keys with the SAME structure however I cant seem to get the subcondition key to be configurable my syntax is'nt quite right.
With the following JSON
{
"conditions": [
{
"condition": [
{
"name": "TOP",
"subcondition": [
{
"name": "CHILD1"
}
]
}
]
}
]
}
I can reference the top level conditions as such using square brackets..
$.getJSON(jsonPath,
function(data) {
$.each(data[topLevel], function(topentryIndex, entry) {
This works OK !
The problematic subsection doesnt seem to work I had prior to this using paramaterized used the following ... The square brackets dont work here ...
$.each(this.subcondition, function(entryIndex, entry) { ....
I've posted a more complete version on the jsfiddle the JSON can be seen in the CSS view.
Reference - http://jsfiddle.net/wmitchell/QRkxd/
Any help much appreciated !
W