json - 将 Typescript 接口(interface)与递归 JSON 结合使用

标签 json typescript recursion ontology

我正在尝试使用 JSON 调整产品的本体及其属性。下面提到的 JSON 结构是我正在考虑的。

Each Product (Concept) has two types of properties: 1. Data Properties 2. Object Properties

使用 Protege 时这些属性的典型定义如下 SO Thread :

In Protégé there are are different tabs for creating Object Properties and Datatype Properties. If a property should relate individuals to individuals, then it needs to be an object property, and if it relates individuals to literals, then it needs to be a datatype property.

我认为每个属性都具有以下属性:

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 等作为 datapropertieshorizo​​ntalsurface立法 作为对象属性

JSON 到 Typescript 接口(interface)

我使用了 JSON to TS Online Converter查看如何将 JSON 转换为 Typescript 接口(interface),结果如下:

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;
}

推理

我推断使用来自递归 JSON 的接口(interface)的方法是不可扩展的,因为这样的产品本体可以扩展到 1000 个属性和属性中的属性。如上面提到的示例所示,对于父属性中的每个属性,将继续创建接口(interface)。

期待

我应该期望使用具有这种 JSON 结构的 Typescript 接口(interface),还是应该坚持创建一个类,然后遵循创建二叉树的传统方法,即。

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

然后写一个递归直到解析完整的结构?

长话短说

Can Typescript interfaces be used for Large Recursive JSON Structures?

最佳答案

您应该能够将该结构很好地表示为递归接口(interface):

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"
        }
        ]
    }
  ]
}`;
}

关于json - 将 Typescript 接口(interface)与递归 JSON 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48286422/

相关文章:

javascript - 在 Angular js 中嵌入来自 json 响应的 Youtube 视频 url 和 Vimeo 视频 url 时的问题

javascript - 如何在html中显示多个json数据

JSON 中 15 个字符的字母数字字符的正则表达式

node.js - 错误: Cannot resolve module 'apply' webpack angular2

json - TypeScript如何将对象数组格式化为json数组

typescript - 如何移动到 VS Code 中的下一个/上一个变量引用?

math - 有人可以解释数学归纳法(证明递归方法)

json - 如何使用 shell jq 动态解析 JSON 对象

recursion - 哪些语言没有循环结构?

java - 我的快速排序算法中的 Stackoverflow 错误