简体   繁体   English

JSON数据到打字稿模型

[英]Json data to typescript model

I have a json file, i want to create the model to use the data list(department number and name) for auto-completion use, i found the site json2ts.com but it doesn't work any more. 我有一个json文件,我想创建模型以使用数据列表(部门编号和名称)进行自动完成使用,我找到了站点json2ts.com,但它不再起作用。 Can you give me an example plese? 能给我一个例子吗?

{
    "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"
        },
     ...
}

Can you give me a suggestion, please? 你能给我一个建议吗? At least for the departments. 至少对于部门而言。 Is it better to create interface or class in this case? 在这种情况下创建接口或类更好吗? Thank you. 谢谢。

For regions, you can use this interface with your current data: 对于区域,您可以将此界面与当前数据一起使用:

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

This will create an interface for an object that can have any string as key, and the value of each property must be an array of numbers (your "regions" property in the above object matches this description). 这将为可以使用任何字符串作为键的对象创建一个接口,并且每个属性的值必须是数字数组(上述对象中的“ regions”属性与该描述匹配)。

For departments, I recommend breaking it up into two interfaces, like so: 对于部门,我建议将其分为两个界面,如下所示:

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

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

Now each member of the "departments" object can have any key as long as it's a string, and must be attached to a department. 现在,“部门”对象的每个成员都可以具有任何键,只要它是字符串即可,并且必须附加到部门。 It looks like your data requires every department to have a name and a formatted_name (no more, no less), which I've represented in the interface Department above. 看来您的数据要求每个部门都有一个名称和一个formatted_name(不多,不少于),我已经在上面的Department表示过。

Then your whole object would look like this: 然后,您的整个对象将如下所示:

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

For VScode IDE (Preferable IDE for Angular Dev), I would suggest you to add plugins which provide functionality of converting JSON to Typescript. 对于VScode IDE (Angular Dev的首选IDE ),我建议您添加一些插件,这些插件提供将JSON转换为Typescript的功能。 Possible solutions can be JSON to TS and JSON to Type . 可能的解决方案可以是JSON到TSJSON到Type

I hope it might helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM