簡體   English   中英

使用帶有遞歸JSON的Typescript接口

[英]Using Typescript Interface with a Recursive JSON

我正在嘗試使用JSON調整產品的本體及其屬性。 下面提到的JSON結構就是我在想的。

每個產品(概念)都有兩種類型的屬性:1。數據屬性2.對象屬性

使用Protege時這些屬性的典型定義如下所示SO Thread

在Protégé中,有不同的選項卡用於創建對象屬性和數據類型屬性。 如果屬性應該將個體與個體相關聯,那么它需要是一個對象屬性,如果它將個體與文字相關聯,那么它需要是一個數據類型屬性。

我認為每個屬性都具有以下屬性:

name: string
url: string
type: dataprop or objprop
objPropSource: available only for Objproperties

我已經制定了一個小的遞歸JSON,如下所示:

{
  "name": "chair",
  "url": "http://namespace.org#chair",
  "type": "main",
  "properties": [
    {
      "name": "height",
      "url": "http://namespace.org#height",
      "type": "dataprop"
    },
    {
      "name": "width",
      "url": "http://namespace.org#width",
      "type": "dataprop"
    },
    {
      "name": "horizontalsurface",
      "url": "http://namespace.org#horizontalsurface",
      "type": "objprop",
      "objPropSource": "http://namespace.org#hasHorizontalSurface",
      "properties": [
        {
          "name": "Legislation",
          "url": "http://namespace.org#legislation",
          "type": "objprop",
          "objPropSource": "http://namespace.org#compliesWithLegislation",
          "properties": [
            {
              "name": "hasLegislationName",
              "url": "http://namespace.org#hasLegislationName",
              "type": "dataprop"
            }
            ]
        }
        ]
    },
    {
      "name": "legislation",
      "url": "http://namespace.org#legislation",
      "type": "objprop",
      "objPropSource": "http://namespace.org#compliesWithLegistion",
      "properties": [
        {
          "name": "hasLegislationName",
          "url": "http://namespace.org#hasLegislationName",
          "type": "dataprop"
        }
        ]
    }
  ]
}

在某種程度上,結構為椅子提供了二進制樹,其具有heightwidth等作為數據性質horizontalsurface以及作為對象性質的 legislation

在Typescript中接口的JSON

我使用JSON到TS Online Converter來查看如何將JSON轉換為Typescript接口,結果如下:

interface RootObject {
  name: string;
  url: string;
  type: string;
  properties: Property3[];
}

interface Property3 {
  name: string;
  url: string;
  type: string;
  objPropSource?: string;
  properties?: Property2[];
}

interface Property2 {
  name: string;
  url: string;
  type: string;
  objPropSource?: string;
  properties?: Property[];
}

interface Property {
  name: string;
  url: string;
  type: string;
}

推理

我推斷,使用Recursive JSON接口的方法是不可擴展的,因為產品的這種Ontology可以在屬性中擴展到1000的屬性和屬性。 如上所述,示例表明,對於父屬性中的每個Property,將繼續創建接口。

期望

我是否應該使用具有此類JSON結構的Typescript接口,或者應該堅持創建一個類,然后遵循創建二叉樹的傳統方法。

export class leaf {
  name: string;
  url: string;
  type: string;
  children: leaf[] = [];
}

然后寫一個遞歸直到整個結構被解析?

TL; DR

Typescript接口可以用於大型遞歸JSON結構嗎?

您應該能夠將該結構表示為遞歸接口:

interface Property {
  name: string;
  url: string;
  type: string;
  objPropSource?: string;
  properties?: Property[];
}

您嘗試使用的JSON到TS轉換器似乎沒有識別結構的遞歸性質的功能。

工作范例:

interface Property {
  name: string;
  url: string;
  type: string;
  objPropSource?: string; // optional property
  properties?: Property[];
};

var p: Property = JSON.parse(getJson());

alert(p.properties[2].properties[0].name);
alert(p.properties[3].objPropSource);




function getJson() {
  return `{
  "name": "chair",
  "url": "http://namespace.org#chair",
  "type": "main",
  "properties": [
    {
      "name": "height",
      "url": "http://namespace.org#height",
      "type": "dataprop"
    },
    {
      "name": "width",
      "url": "http://namespace.org#width",
      "type": "dataprop"
    },
    {
      "name": "horizontalsurface",
      "url": "http://namespace.org#horizontalsurface",
      "type": "objprop",
      "objPropSource": "http://namespace.org#hasHorizontalSurface",
      "properties": [
        {
          "name": "Legislation",
          "url": "http://namespace.org#legislation",
          "type": "objprop",
          "objPropSource": "http://namespace.org#compliesWithLegislation",
          "properties": [
            {
              "name": "hasLegislationName",
              "url": "http://namespace.org#hasLegislationName",
              "type": "dataprop"
            }
            ]
        }
        ]
    },
    {
      "name": "legislation",
      "url": "http://namespace.org#legislation",
      "type": "objprop",
      "objPropSource": "http://namespace.org#compliesWithLegistion",
      "properties": [
        {
          "name": "hasLegislationName",
          "url": "http://namespace.org#hasLegislationName",
          "type": "dataprop"
        }
        ]
    }
  ]
}`;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM