JSON

example of recursive JSON handling

by chrisloughnane

JavaScript

var yourObj = {
    "baseball": {
        "mlb": {
            "regular": {
                "_events": [{
                    "start_time": "2011-07-31 17:35",
                    "lines": [{
                        "comment": "",
                        "coeff": "2.35",
                        "title": "2",
                        "old_coeff": "2.35",
                        "is_main": true},
                    {
                        "comment": "",
                        "coeff": "1.59",
                        "title": "2",
                        "old_coeff": "1.59",
                        "is_main": true},
                    {
                        "comment": "",
                        "coeff": "1.59",
                        "title": "2",
                        "old_coeff": "1.59",
                        "is_main": true},
                    {
                        "comment": "",
                        "coeff": "2.35",
                        "title": 2,
                        "old_coeff": "2.35",
                        "is_main": true}],
                    "members": ["atlanta", "florida"]}
                                                                          ]
            }
        }
    }
};

function recursiveGetProp(obj, lookup, callback) {
    for (property in obj) {
        if (property == lookup) {
            callback(obj[property]);
        } else if (obj[property] instanceof Object) {
            recursiveGetProp(obj[property], lookup, callback);
        }
    }
}

recursiveGetProp(yourObj, '_events', function(obj) {
    console.debug(obj);
});