简体   繁体   English

如何从 Json 数据创建 TypeScript 类?

[英]How to create TypeScript class from Json data?

I'm using Angular to call an external API.我正在使用 Angular 调用外部 API。 Json data is in format like: Json 数据的格式如下:

[
      {
        "AccessGroupsIdList": [],
        "FirstName": "Greg",
        "LastName": "Tipton",
        "LocationIdList": [],
        "PermissionProfile": {
          "Name": "Agent",
          "PermissionProfileId": {
            "ID": "xy678219-bd7c-103d-b56b-1f1234a85990"
          },
          "Type": 3
        },
        "ManagerName": "Gilchrist, George",
        "Status": true,
        "UserGroupID": {
          "ID": "00000000-0000-0000-0000-000000000000"
        },
        "UserGroupName": "ROOT",
        "UserId": {
          "ID": "4445cc66-819a-4da0-8fbf-d0bb8ce65941"
        }
      }
    ]

How do I create a class in typescript to read it since json data is nested?由于 json 数据是嵌套的,如何在打字稿中创建一个类来读取它?

export class Employees
{
AccessGroupsIdList: string[];
FirstName: string;
LastName: string;
LocationIdList : number[];
PermissionProfile ??
ManagerName: string;
Status: boolean;
UserGroupID ??
UserGroupName : string;
UserId ??
}

Please guide if the PermissionProfile, PermissionProfile will be separate nested classes?请指导如果PermissionProfile, PermissionProfile 会单独嵌套类吗? How do I declare those?我如何声明这些?

Try declaring the Typescript class structures as follows:尝试如下声明 Typescript 类结构:

export class Employees
{
    AccessGroupsIdList: string[];
    FirstName: string;
    LastName: string;
    LocationIdList : number[];
    PermissionProfile: PermissionProfile;
    ManagerName: string;
    Status: boolean;
    UserGroupId: UserGroupID;
    UserGroupName : string;
    UserId: UserID;
}

export class PermissionProfile 
{
    name: string;
    permissionProfileId: PermissionProfileID;
    type: string;
}

export class PermissionProfileID
{
    id: string;
}
 
export class UserGroupID
{
    id: string;
}

export class UserID
{
    id: string;
}

I would suggest to name the property names consistently with an Id (eg with UserGroupId ).我建议使用 Id 命名属性名称(例如使用UserGroupId )。 The name and type class property names are valid in TypeScript (unlike with the C# syntax). nametype类属性名称在 TypeScript 中有效(与 C# 语法不同)。

To extend Andrew Halil's answer, I would use interfaces instead of classes in your definitions, since there do not appear to be any class methods involved;为了扩展 Andrew Halil 的回答,我将在您的定义中使用接口而不是类,因为似乎没有涉及任何类方法; you are just describing the shape of a JSON object returned from a server您只是在描述从服务器返回的 JSON 对象的形状

export interface Employee
{
    AccessGroupsIdList: string[];
    FirstName: string;
    LastName: string;
    LocationIdList : number[];
    PermissionProfile: PermissionProfile;
    ManagerName: string;
    Status: boolean;
    UserGroupId: ID;
    UserGroupName : string;
    UserId: ID;
}

export interface PermissionProfile 
{
    name: string;
    permissionProfileId: ID;
    type: string;
}

export interface ID
{
    id: string;
}
 

Now as for an implementation, I don't use Angular all that much but you would do something like this to get the items typed现在至于实现,我并没有那么多地使用 Angular,但是你会做这样的事情来输入项目


async function listEmployees(): Promise<Employee[]> {
// Make a fetch call to the API endpoint
   const data = await fetch('https://some-api-endpoint.web/employees')
      // if the response comes back ok, return the JSON-ified response.
      .then(res => {
          if(res.ok) return res.json()
          return [];
      });
    // Instruct typescript that "data" is to be treated as an array of Employee elements.
    return data as Employee[]
}

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

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