[英]Using Typescript Interface with a Recursive JSON
I am trying to adapt a product's ontology with its properties using JSON. 我正在尝试使用JSON调整产品的本体及其属性。 The below mentioned JSON structure is what I am thinking about.
下面提到的JSON结构就是我在想的。
Each Product (Concept) has two types of properties: 1. Data Properties 2. Object Properties
每个产品(概念)都有两种类型的属性:1。数据属性2.对象属性
Typical Definitions for these properties when using Protege is as follows SO Thread : 使用Protege时这些属性的典型定义如下所示SO Thread :
In Protégé there are are different tabs for creating Object Properties and Datatype Properties.
在Protégé中,有不同的选项卡用于创建对象属性和数据类型属性。 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.
如果属性应该将个体与个体相关联,那么它需要是一个对象属性,如果它将个体与文字相关联,那么它需要是一个数据类型属性。
I think of every property to have the following attributes: 我认为每个属性都具有以下属性:
name: string
url: string
type: dataprop or objprop
objPropSource: available only for Objproperties
I have drawn up a small recursive JSON as below: 我已经制定了一个小的递归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"
}
]
}
]
}
In a way structure provides a Binary Tree for a chair which has height
, width
etc. as dataproperties and horizontalsurface
and legislation
as objectproperties 在某种程度上,结构为椅子提供了二进制树,其具有
height
, width
等作为数据性质和horizontalsurface
以及作为对象性质的 legislation
I used the JSON to TS Online Converter to see how will the JSON be converted to Typescript Interfaces and the outcome is following: 我使用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;
}
I infer that the aprroach for using Interfaces from Recursive JSON is not scalable since such an Ontology of a Product can scale upto 1000's of properties and properties within properties. 我推断,使用Recursive JSON接口的方法是不可扩展的,因为产品的这种Ontology可以在属性中扩展到1000的属性和属性。 As above mentioned example show that for every Property within a parent property would keep creating interfaces.
如上所述,示例表明,对于父属性中的每个Property,将继续创建接口。
Should I expect to use Typescript Interfaces with such JSON structure or should to stick to creating a Class and then following the conventional method of creating a Binary Tree viz. 我是否应该使用具有此类JSON结构的Typescript接口,或者应该坚持创建一个类,然后遵循创建二叉树的传统方法。
export class leaf {
name: string;
url: string;
type: string;
children: leaf[] = [];
}
and then writing a recursion till the complete structure is parsed? 然后写一个递归直到整个结构被解析?
Can Typescript interfaces be used for Large Recursive JSON Structures?
Typescript接口可以用于大型递归JSON结构吗?
You should be able to represent that structure just fine as a recursive interface: 您应该能够将该结构表示为递归接口:
interface Property {
name: string;
url: string;
type: string;
objPropSource?: string;
properties?: Property[];
}
It appears that the JSON to TS converter you tried to use just doesn't have the functionality to recognize the recursive nature of your structure. 您尝试使用的JSON到TS转换器似乎没有识别结构的递归性质的功能。
Working example: 工作范例:
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.