【问题标题】:how to create nested list using flat JSON data [closed]如何使用平面 JSON 数据创建嵌套列表 [关闭]
【发布时间】:2017-06-13 12:37:52
【问题描述】:

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 > CountryName

【问题讨论】:

  • 把你的 JSON 放入一个代码 sn-p
  • 向我们展示您的尝试。 SO 不是一个代码订购服务,您只需放弃您的要求。
  • 所以,你需要一些 js、php、jquery(js) ......或者这是某种调查?

标签: javascript php jquery json grouping


【解决方案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; }

相同但不使用thisArg of Array#forEach

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)迭代值 2)减少键数组。 3) 将 ForEach 的执行上下文设置为对象键“_”值“结果”空数组。 4)。利用它的“this”推入 forEach 内部的空数组。 5) 将包含国家名称及其父 countryId 的对象推入“结果”。 5)然后检查是否有一个数组,其国家名称由您的键数组散列 6)如果没有具有该散列名称的数组文字,则为它分配一个对象键属性空数组值,您在其中推送名称子项属性?
  • 这个函数可以用来创建列表吗?
  • @arrow,对,你理解正确。如果您愿意,可以将 this 替换为对 dame 值的闭包。
  • @Kiranm,对,它有助于使用键创建嵌套结构,该结构遵循每个级别的 sam 构建模式。最后一层可以包含自定义结构。
【解决方案2】:

您可以为此使用 JSON.parse() 函数。 假设您在变量 data.json 中有这个 json。 你可以做类似的事情

var parsedData = JSON.parse(data);

这将为您提供一个漂亮的 javascript 对象,其中包含所有必需的嵌套。

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

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

【讨论】:

  • OP 没有说明是问js还是php解决方案。不要回答这样不好的问题。
  • 我认为它是来自标签的 javascript。这里是一个完整的 stackoverflow 新手。
【解决方案3】:

尝试下一个(如果它与 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2020-08-31
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2018-11-24
    • 2019-11-27
    相关资源
    最近更新 更多