Convert JSON object to a class in TypeScript
Data on the internet flows in the form of strings, which can be converted into a very popular format called JSON. The JSON representation of this data usually represents an object or even a class in TypeScript.
TypeScript provides the ability to convert objects into classes. This article will discuss how to convert received objects into TypeScript classes to make type support, IDE completion, and other features accessible.
Object.assign
Convert from JSON to class in TypeScript
Object.assign
The method provides an easy way to convert a JSON object to a TypeScript class so that the methods associated with that class can also be accessed.
The following code snippet shows how to use Object.assign
the method to convert a JSON object to a TypeScript class.
Code:
class Animal{
name : string;
legs : number;
eyes : number;
constructor(name? : string, legs? : number, eyes? : number){
this.name = name ?? 'Default name';
this.legs = legs ?? 0;
this.eyes = eyes ?? 0;
}
getName(){
return this.name;
}
getEyes(){
return this.eyes;
}
}
var jsonString : string = `{
"name" : "Tiger",
"legs" : 4,
"eyes" : 2
}`
var animalObj = new Animal();
console.log(animalObj.getEyes());
Object.assign(animalObj, JSON.parse(jsonString));
console.log(animalObj.getEyes());
Output:
0
2
In the above example, getEyes()
the method returns 0
, which is the default value, and when the parsed JSON object is assigned to animalObj
the object, it returns 2
, the value in the JSON string.
Convert JSON string to class using custom method in TypeScript
A JSON object can fromJSON
be converted to the corresponding class in TypeScript using a custom method such as This approach is more useful as it provides more power to the user.
Code:
class Animal{
name : string;
legs : number;
eyes: number;
constructor(name : string, legs : number, eyes: number){
this.name = name;
this.legs = legs;
this.eyes = eyes;
}
getName(){
return this.name;
}
toObject(){
return {
name : this.name,
legs : this.legs.toString(),
eyes : this.eyes.toString()
}
}
serialize() {
return JSON.stringify(this.toObject());
}
static fromJSON(serialized : string) : Animal {
const animal : ReturnType<Animal["toObject"]> = JSON.parse(serialized);
return new Animal(
animal.name,
animal.legs,
animal.eyes
)
}
}
var jsonString : string = `{
"name" : "Tiger",
"legs" : 4,
"eyes" : 2
}`
var animalObj : Animal = Animal.fromJSON(jsonString);
console.log(animalObj)
// this will work now
console.log(animalObj.getName());
Output:
Animal: {
"name": "Tiger",
"legs": 4,
"eyes": 2
}
"Tiger"
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Handling exceptions with try..catch..finally in TypeScript
Publish Date:2025/04/15 Views:170 Category:TypeScript
-
This article will discuss try..catch..finally handling exceptions in TypeScript using the statement. Handling exceptions in TypeScript In TypeScript, try..catch..finally a block handles exceptions that occur during program runtime. It allow
在 TypeScript 中使用 try..catch..finally 处理异常
Publish Date:2023/03/19 Views:396 Category:TypeScript
-
本文详细介绍了如何在 TypeScript 中使用 try..catch..finally 进行异常处理,并附有示例。
在 TypeScript 中使用 declare 关键字
Publish Date:2023/03/19 Views:257 Category:TypeScript
-
本教程指南通过特定的实现和编码示例深入了解了 TypeScript 中 declare 关键字的用途。
在 TypeScript 中 get 和 set
Publish Date:2023/03/19 Views:971 Category:TypeScript
-
本篇文章演示了类的 get 和 set 属性以及如何在 TypeScript 中实现它。
在 TypeScript 中格式化日期和时间
Publish Date:2023/03/19 Views:276 Category:TypeScript
-
本教程介绍内置对象 Date() 并讨论在 Typescript 中获取、设置和格式化日期和时间的各种方法。
在 TypeScript 中返回一个 Promise
Publish Date:2023/03/19 Views:590 Category:TypeScript
-
本教程讨论如何在 TypeScript 中返回正确的 Promise。这将提供 TypeScript 中 Returns Promise 的完整编码示例,并完整演示每个步骤。
在 TypeScript 中定义函数回调的类型
Publish Date:2023/03/19 Views:1454 Category:TypeScript
-
本教程说明了在 TypeScript 中为函数回调定义类型的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。
在 TypeScript 中把 JSON 对象转换为一个类
Publish Date:2023/03/19 Views:530 Category:TypeScript
-
本教程演示了如何将 JSON 对象转换为 TypeScript 中的类。
使用 NPM 将 TypeScript 更新到最新版本
Publish Date:2023/03/19 Views:455 Category:TypeScript
-
本教程说明了如何使用 npm 更新到最新版本的 TypeScript。这将为如何使用 npm 将 TypeScript 更新到最新版本提供完整的实际示例。