JSON Parsing and Generation with ArkTS
Module Introduction
This module facilitates the conversion of JSON text to corresponding ArkTS objects or values and vice - versa.
Importing the Module
import { JSON } from '@kit.ArkTS';
JSON Parsing
Using JSON.parse
parse(text: string, reviver?: Transformer, options?: ParseOptions): Object | null
This function parses a JSON string to generate the corresponding ArkTS object or null.
- ** Supported from API version 12 in meta - services.
- System Capability: SystemCapability.Utils.Lang
Parameters:
Parameter Name | Type | Required | Description |
---|---|---|---|
text | string | Yes | A valid JSON string. |
reviver | Transformer | No | A transformation function that can modify the original values generated by parsing. Default: undefined. |
options | ParseOptions | No | Configuration for parsing that can control the generated type. Default: undefined. |
Return Value:
Type | Description |
---|---|
Object | null |
Error Codes:
Error Code ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are missing; 2. Incorrect parameter types; 3. Parameter verification failed. |
Example:
let jsonText = '{"name": "John", "age": 30, "city": "ChongQing"}';
let obj = JSON.parse(jsonText);
console.info("getname --- > " + (obj as object)?.["name"]);
console.info("getage --- > " + (obj as object)?.["age"]);
console.info("getcity --- > " + (obj as object)?.["city"]);
In this example, we use the JSON.parse method to obtain the obj object and then retrieve values using (obj as object)?.["name"].
Example with interface:
interface Person {
name: string;
age: number;
city: string;
}
let jsonText = '{"name": "John", "age": 30, "city": "ChongQing"}';
let person: Person = JSON.parse(jsonText) as Person;
console.info("personinterface getpersonname --- > " + person.name);
console.info("personinterface getpersonage --- > " + person.age);
console.info("personinterface getpersoncity --- > " + person.city);
Example with class:
class Persontest {
name: string = '';
age: number = 0;
city: string = '';
}
let jsonText = '{"name": "John", "age": 30, "city": "ChongQing"}';
let persontest: Persontest = JSON.parse(jsonText.toString()) as Persontest;
console.info("personclass getpersontest --- > " + persontest.name);
console.info("personclass getpersontest --- > " + persontest.age);
console.info("personclass getpersontest --- > " + persontest.city);
Converting Objects or Arrays to JSON - JSON.stringify
This method converts an ArkTS object or array to a JSON string, supporting linear container conversion. Non - linear containers are not supported.
- ** Supported from API version 12 in meta - services.
- System Capability: SystemCapability.Utils.Lang
Parameters:
Parameter Name | Type | Required | Description |
---|---|---|---|
value | Object | Yes | The ArkTS object or array to be converted. Linear containers are supported for conversion, while non - linear containers are not. |
replacer | Transformer | No | During serialization, each property of the value being serialized is transformed and processed by this function. Default: undefined. |
space | string | number | No |
Return Value:
Type | Description |
---|---|
string | The converted JSON string. |
Example:
interface Person {
name: string;
age: number;
city: string;
}
let testobj = { "name": "John", "age": 30, "city": "ChongQing" } as Person;
let str1 = JSON.stringify(testobj);
console.info("jsonstringify getstr1 --- > " + str1);
Top comments (0)