如何使用平面JSON数据创建嵌套列表

时间:2017-06-13 12:37:53

标签: javascript php jquery json grouping

JSON

[
{"CountryID":1,"CountryName":"Austria","CountryG2":"Austria","CountryG3":"DACH","CountryG4":"Western Europe"},
{"CountryID":2,"CountryName":"Germany","CountryG2":"Germany","CountryG3":"DACH","CountryG4":"Western Europe"},
{"CountryID":3,"CountryName":"Ireland, United Kingdom","CountryG2":"UK&I","CountryG3":"UK&I","CountryG4":"Western Europe"},
{"CountryID":4,"CountryName":"Belarus","CountryG2":"Belarus","CountryG3":"ESE","CountryG4":"CEMA"},
{"CountryID":5,"CountryName":"Bosnia","CountryG2":"Bosnia","CountryG3":"ESE","CountryG4":"CEMA"},
{"CountryID":6,"CountryName":"Serbia","CountryG2":"Serbia","CountryG3":"ESE","CountryG4":"CEMA"},
{"CountryID":7,"CountryName":"Croatia","CountryG2":"Croatia","CountryG3":"ESE","CountryG4":"CEMA"},
{"CountryID":8,"CountryName":"Greece","CountryG2":"Greece","CountryG3":"ESE","CountryG4":"CEMA"},
{"CountryID":9,"CountryName":"Poland","CountryG2":"Poland","CountryG3":"Central Europe","CountryG4":"CEMA"},
{"CountryID":10,"CountryName":"Czech Republic","CountryG2":"Czech Republic","CountryG3":"Central Europe","CountryG4":"CEMA"},
{"CountryID":12,"CountryName":"Slovakia","CountryG2":"Slovakia","CountryG3":"Central Europe","CountryG4":"CEMA"},
{"CountryID":13,"CountryName":"Hungary","CountryG2":"Hungary","CountryG3":"Central Europe","CountryG4":"CEMA"},
{"CountryID":14,"CountryName":"Israel","CountryG2":"Israel","CountryG3":"Central Europe","CountryG4":"CEMA"},
{"CountryID":15,"CountryName":"Russia","CountryG2":"Russia","CountryG3":"Russia","CountryG4":"CEMA"}
]

分组 CountryG4> CountryG3> CountryG2>国家或地区名称

3 个答案:

答案 0 :(得分:1)

您可以使用嵌套哈希表方法。

var data = [{ CountryID: 1, CountryName: "Austria", CountryG2: "Austria", CountryG3: "DACH", CountryG4: "Western Europe" }, { CountryID: 2, CountryName: "Germany", CountryG2: "Germany", CountryG3: "DACH", CountryG4: "Western Europe" }, { CountryID: 3, CountryName: "Ireland, United Kingdom", CountryG2: "UK&I", CountryG3: "UK&I", CountryG4: "Western Europe" }, { CountryID: 4, CountryName: "Belarus", CountryG2: "Belarus", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 5, CountryName: "Bosnia", CountryG2: "Bosnia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 6, CountryName: "Serbia", CountryG2: "Serbia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 7, CountryName: "Croatia", CountryG2: "Croatia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 8, CountryName: "Greece", CountryG2: "Greece", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 9, CountryName: "Poland", CountryG2: "Poland", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 10, CountryName: "Czech Republic", CountryG2: "Czech Republic", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 12, CountryName: "Slovakia", CountryG2: "Slovakia", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 13, CountryName: "Hungary", CountryG2: "Hungary", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 14, CountryName: "Israel", CountryG2: "Israel", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 15, CountryName: "Russia", CountryG2: "Russia", CountryG3: "Russia", CountryG4: "CEMA" }],
    keys = ['CountryG4', 'CountryG3', 'CountryG2'],
    result = [];

data.forEach(function (a) {
    keys.reduce(function (r, k) {
        if (!r[a[k]]) {
            r[a[k]] = { _: [] };
            r._.push({ name: a[k], children: r[a[k]]._ });
        }
        return r[a[k]];
    }, this)._.push({ CountryID: a.CountryID, CountryName: a.CountryName });
}, { _: result });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

相同但未使用Array#forEach

thisArg

var data = [{ CountryID: 1, CountryName: "Austria", CountryG2: "Austria", CountryG3: "DACH", CountryG4: "Western Europe" }, { CountryID: 2, CountryName: "Germany", CountryG2: "Germany", CountryG3: "DACH", CountryG4: "Western Europe" }, { CountryID: 3, CountryName: "Ireland, United Kingdom", CountryG2: "UK&I", CountryG3: "UK&I", CountryG4: "Western Europe" }, { CountryID: 4, CountryName: "Belarus", CountryG2: "Belarus", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 5, CountryName: "Bosnia", CountryG2: "Bosnia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 6, CountryName: "Serbia", CountryG2: "Serbia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 7, CountryName: "Croatia", CountryG2: "Croatia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 8, CountryName: "Greece", CountryG2: "Greece", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 9, CountryName: "Poland", CountryG2: "Poland", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 10, CountryName: "Czech Republic", CountryG2: "Czech Republic", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 12, CountryName: "Slovakia", CountryG2: "Slovakia", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 13, CountryName: "Hungary", CountryG2: "Hungary", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 14, CountryName: "Israel", CountryG2: "Israel", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 15, CountryName: "Russia", CountryG2: "Russia", CountryG3: "Russia", CountryG4: "CEMA" }],
    keys = ['CountryG4', 'CountryG3', 'CountryG2'],
    result = [];

data.forEach(function (hash) {
    return function (a) {
        keys.reduce(function (r, k) {
            if (!r[a[k]]) {
                r[a[k]] = { _: [] };
                r._.push({ name: a[k], children: r[a[k]]._ });
            }
            return r[a[k]];
        }, hash)._.push({ CountryID: a.CountryID, CountryName: a.CountryName });
    };
}({ _: result }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

您可以使用JSON.parse()函数。 假设你在变量数据中有这个json。 你可以做点什么

var parsedData = JSON.parse(data);

这将为您提供一个很好的javascript对象,包含所有必需的嵌套。

如果您将此JSON保存在node.js上的文件中,则可以执行类似

的操作
var parsedData = JSON.parse(fs.readFileSync("filename.json"));

答案 2 :(得分:0)

尝试下一个(如果它与PHP相关):

$data = [
    ["CountryID" => 1, ... // put your array here
];
// or use: $data = json_decode($jsonString, true);

$store = array();
foreach ($data as $val) {
    $store[$val['CountryG4']][$val['CountryG3']][$val['CountryG2']] = $val['CountryName'];
}

var_dump($store);

如果是JavaScript问题:

var store = {};
json.forEach(function (data) {
    store[data.CountryG4][data.CountryG3][data.CountryG2] = data.CountryName;
});

console.log(store);