不敢表白的葫芦 · JSDoc and TypeScript ...· 昨天 · |
冲动的香菇 · TypeScript 1.5 - ...· 昨天 · |
俊秀的冲锋衣 · 功能 | Vite 官方中文文档· 昨天 · |
含蓄的弓箭 · TypeScript 中的映射类型修饰符 ...· 昨天 · |
彷徨的绿茶 · typescript,把一个对象数组转换成m ...· 3 天前 · |
逃跑的面包 · 在?进来看一下“赛里木湖·蓝” _大公网· 1 月前 · |
温文尔雅的橡皮擦 · 只要你会前端的vue框架就可以开发电脑桌面应 ...· 2 月前 · |
爽快的莲藕 · 交通信息工程系赴竹林松大科技股份有限公司商谈 ...· 8 月前 · |
读研的木耳 · 54岁郭冬临教育孩子遭嫌弃,“女儿”大眼机灵 ...· 1 年前 · |
追风的凳子 · 天主教普照中學| Po Chiu ...· 1 年前 · |
django 缂栫▼璇█ string typescript |
https://www.geeksforgeeks.org/how-to-parse-json-string-in-typescript/ |
慈祥的萝卜
7 月前 |
In this tutorial, we will learn how we can parse a JSON string in TypeScript. The main reason for learning about it is to learn how we can explicitly type the resulting string to a matching type. The JSON.parse() method will be used to parse the JSON string by passing the parsing string as a parameter to it.
JSON.parse(parsingString);
We can type the parsed string with an explicit type using the following methods:
Table of Content
The type alias will be used to define a new type with a mixture of different typed properties and can be used to type the parsed string.
Example: The below example will show how you can explicitly type the parsed string using type alias.
const jsonStr1 =
'{"name": "GeeksforGeeks", "desc": "A Computer Science Portal for Geeks"}'
;
const jsonStr2 =
'{"name": "Google", "desc": "Searching Platform", "workforce": 2000}'
;
type parseType = {
name: string,
desc: string,
workforce?: number
const parsedStr1: parseType = JSON.parse(jsonStr1);
const parsedStr2: parseType = JSON.parse(jsonStr2);
console.log(`Company Name:
${parsedStr1.name},
Description:
${parsedStr1.desc}`);
console.log(`Company Name:
${parsedStr2.name},
Description: ${parsedStr2.desc},
Work Force: ${parsedStr2.workforce}`);
Output:
Company Name: GeeksforGeeks, Description: A Computer Science Portal for Geeks
Company Name: Google, Description: Searching Platform, Work Force: 2000
The interfaces can also be used to type the parsed string to the required type as shown in the below example.
Example: This example shows the parsing of JSON string using interface.
const jsonStr1 =
'{"name": "GeeksforGeeks", "desc": "A Computer Science Portal for Geeks"}'
;
const jsonStr2 =
'{"name": "Google", "desc": "Searching Platform", "workforce": 2000}'
;
interface parseInterface {
name: string,
desc: string,
workforce?: number
const parsedStr1: parseInterface = JSON.parse(jsonStr1);
const parsedStr2: parseInterface = JSON.parse(jsonStr2);
console.log(`Company Name:
${parsedStr1.name},
Description:
${parsedStr1.desc}`);
console.log(`Company Name:
${parsedStr2.name},
Description: ${parsedStr2.desc},
Work Force: ${parsedStr2.workforce}`);
Output:
Company Name: GeeksforGeeks, Description: A Computer Science Portal for Geeks
Company Name: Google, Description: Searching Platform, Work Force: 2000
An array string can also be parsed using the parse method and required to be typed as an explicit array of required types.
const parsedString = JSON.parse(string) as createdType[];
Example: The below example will explain how to type an parsed array string.
const jsonStr =
{
"name"
:
"GeeksforGeeks"
,
"desc"
:
"A Computer Science Portal for Geeks"
},
{
"name"
:
"Google"
,
"desc"
:
"Searching Platform"
,
"workforce"
: 2000}
type parsedType = {
name: string,
desc: string,
workforce?: number
const parsedStr = JSON.parse(jsonStr) as parsedType[];
console.log(`Company Name:
${parsedStr[0].name},
Description:
${parsedStr[0].desc}`);
console.log(`Company Name:
${parsedStr[1].name},
Description:
${parsedStr[1].desc}`);
不敢表白的葫芦 · JSDoc and TypeScript IntelliSense support for @link inline tag · Issue #57495 · microsoft/vscode · G 昨天 |
俊秀的冲锋衣 · 功能 | Vite 官方中文文档 昨天 |
逃跑的面包 · 在?进来看一下“赛里木湖·蓝” _大公网 1 月前 |