gpt4 book ai didi

javascript - Json 数据到 typescript 模型

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:27 24 4
gpt4 key购买 nike

我有一个 json 文件,我想创建模型以使用数据列表(部门编号和名称)进行自动完成,我找到了 json2ts.com 网站,但它不再有效。你能举个例子吗?

{
"regions": {
"alsace": [67, 68],
"aquitaine": [40, 47, 33, 24, 64],
"auvergne": [43, 3, 15, 63],
"basse-normandie": [14, 61, 50],
"bourgogne": [21, 58, 71, 89],
"bretagne": [29, 35, 22, 56],
"centre": [45, 37, 41, 28, 36, 18],
"champagne-ardenne": [10, 8, 52, 51],
"corse": ["2b", "2a"],
"franche-compte": [39, 25, 70, 90],
"haute-normandie": [27, 76],
"languedoc-roussillon": [48, 30, 34, 11, 66],
"limousin": [19, 23, 87],
"lorraine": [55, 54, 57, 88],
"midi-pyrennees": [46, 32, 31, 12, 9, 65, 81, 82],
"nord-pas-de-calais": [62, 59],
"pays-de-la-loire": [49, 44, 72, 53, 85],
"picardie": [2, 60, 80],
"poitou-charentes": [17, 16, 86, 79],
"provences-alpes-cote-dazur": [4, 5, 6, 13, 84, 83],
"rhones-alpes": [38, 42, 26, 7, 1, 74, 73, 69],
"ile-de-france": [77, 75, 78, 93, 92, 91, 95, 94]
},
"departments": {
"2a": {
"name": "Corse-du-Sud",
"formatted_name": "corse-du-sud"
},
"2b": {
"name": "Haute-Corse",
"formatted_name": "haute-corse"
},
"01": {
"name": "Ain",
"formatted_name": "ain"
},
"02": {
"name": "Aisne",
"formatted_name": "aisne"
},
"03": {
"name": "Allier",
"formatted_name": "allier"
},
...
}

你能给我一个建议吗?至少对于部门而言。在这种情况下创建接口(interface)或类更好吗?谢谢。

最佳答案

对于区域,您可以将此界面与当前数据一起使用:

interface Regions {
[name: string]: number[];
}

这将为一个对象创建一个接口(interface),该对象可以任何字符串作为键,每个属性的值必须是一个数字数组(上面对象中的“区域”属性与此描述相匹配)。

对于部门,我建议将其分成两个界面,如下所示:

interface Departments {
[id: string]: Department;
}

interface Department {
name: string;
formatted_name: string;
}

现在“部门”对象的每个成员都可以有任何键,只要它是一个字符串,并且必须附加到一个部门。看起来您的数据要求每个部门都有一个名称和一个 formatted_name(不多也不少),我在上面的界面 Department 中表示了这一点。

那么你的整个对象看起来像这样:

interface WholeThing {
regions: Regions;
departments: Departments;
}

关于javascript - Json 数据到 typescript 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48285040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com