是否可以编写实用程序函数来解析此JSON格式?

时间:2014-06-05 17:20:03

标签: jquery

{
    "item": {
        "T1": [{
            "name": "Snacks",
                "T2": [{
                "name": "Popcorn",
                    "T3": [{
                    "name": "Packet",
                        "T4": [{
                        "leaf": [{
                            "name": "popcorn packet caramel",
                                "price": 20
                        }, {
                            "name": "popcorn packet cheese",
                                "price": 30
                        }]
                    }]
                }]
            }]
        }, {
            "name": "Beverages",
                "T2": [{
                "name": "Pepsi",
                    "T3": [{
                    "name": "Bottle",
                        "leaf": [{
                        "name": "pepsi bottle 250ml",
                            "price": 50
                    }, {
                        "name": "pepsi bottle 350ml",
                            "price": 60
                    }]
                }, {
                    "name": "Can",
                        "leaf": [{
                        "name": "pepsi can 250ml",
                            "price": 50
                    }, {
                        "name": "pepsi can 100ml",
                            "price": 40
                    }]
                }]
            }]
        }]
    }
}

如果你观察到上面的JSON,每个数组都有名称属性,除了 leaf 级别

例如

T1有两个名字零食和饮料

Snacks有一个名字爆米花

爆米花有一个名字数据包

==========================

Beverages有两个名字百事可乐

百事可乐有两个名字瓶子和罐头

如果给出了一个值,我就用这种方式写了一个函数来返回值

$(document).ready(function () {
    for (var a = 0; a < jsondata.item.T1.length; a++) {
        if (jsondata.item.T1[a].name == 'Snacks') {
            for (var b = 0; b < jsondata.item.T1[a].T2.length; b++) {
                for (var c = 0; c < jsondata.item.T1[a].T2[b].T3.length; c++) {
                    for (var d = 0; d < jsondata.item.T1[a].T2[b].T3[c].T4.length; d++) {
                        for (var e = 0; e < jsondata.item.T1[a].T2[b].T3[c].T4[d].leaf.length; e++) {
                            console.log(jsondata.item.T1[a].T2[b].T3[c].T4[d].leaf[e].name);
                        }
                    }
                }
            }
        }
    }
});

我的问题:是否可以编写一个实用函数,它接受一个值并返回给它的值?

1 个答案:

答案 0 :(得分:1)

玩了一下,但这很有效:

<强> DEMO

function isArray(what) {
    return Object.prototype.toString.call(what) === '[object Array]';
}

function isObject(what) {
    return Object.prototype.toString.call(what) === '[object Object]';
}

var results = [];
var cancel = false;

function recursiveSearch(name, json, startSaving, parentJson) {
    if (cancel) return;
    if (startSaving) {
        if (parentJson["leaf"]) {
            results.push(parentJson["leaf"]);
            cancel = true; //pushing leaf twice for somereason, work around with 'cancel'
            return;
        } else if (json["leaf"]) {
            results.push(json["leaf"]);
            cancel = true; //pushing leaf twice for somereason, work around with 'cancel'
            return;
        } else if (json["name"]) {
            results.push(json["name"]);
            return;
        }
    }
    if (isArray(json)) {
        for (var i = 0; i < json.length; i++) {
            recursiveSearch(name, json[i], startSaving, json);
        }
    } else {
        if (isObject(json)) {
            for (key in json) {
                if (key == "name") {
                    if (json[key] == name) {
                        startSaving = true;
                    }
                } else if (key == name) {
                    startSaving = true;
                }
                recursiveSearch(name, json[key], startSaving, json);
            }
        }
    }
}

function search(name) {
    results = [];
    cancel = false;
    recursiveSearch(name, jsondata);
    console.log(results);
}

console.log('Search for "T1"');
search('T1');
console.log('Search for "T2"');
search('T2');
console.log('Search for "T3"');
search('T3');
console.log('Search for "Snacks"');
search('Snacks');
console.log('Search for "Popcorn"');
search('Popcorn');
console.log('Search for "Packet"');
search('Packet');
console.log('Search for "Beverages"');
search('Beverages');
console.log('Search for "Pepsi"');
search('Pepsi');
console.log('Search for "Bottle"');
search('Bottle');
console.log('Search for "Can"');
search('Can');