1# Performing JSON Operations Using JSVM-API 2 3## Introduction 4 5Data in JavaScript Object Notation (JSON) is used for data transfer, storage, and exchange between the frontend and backend. JSON is language-independent, which means it can be used with multiple programming languages. 6 7## Basic Concepts 8 9JSON: a text-based format for representing structured data. It is widely used for data processing in JS. 10 11## Available APIs 12 13| API | Description | 14|----------------------------|--------------------------------| 15| OH_JSVM_JsonParse | Parses a JSON string and returns the parsed value. | 16| OH_JSVM_JsonStringify | Converts a JS object into a JSON string and returns the converted string. | 17 18## Example 19 20If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ and ArkTS code related to JSON operations. 21 22### OH_JSVM_JsonParse 23 24Use **OH_JSVM_JsonParse** to parse a JSON string and return a valid value of the parsing result. 25 26CPP code: 27 28```cpp 29// hello.cpp 30#include "napi/native_api.h" 31#include "ark_runtime/jsvm.h" 32#include <hilog/log.h> 33// Register JsonParse callbacks. 34static JSVM_CallbackStruct param[] = { 35 {.data = nullptr, .callback = JsonParseNumber}, 36 {.data = nullptr, .callback = JsonParseObject}, 37}; 38static JSVM_CallbackStruct *method = param; 39// Expose theJsonParse callbacks to JS. 40static JSVM_PropertyDescriptor descriptor[] = { 41 {"jsonParseNumber", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 42 {"jsonParseObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 43}; 44// Define OH_JSVM_JsonParse methods. 45 46// Parse the number in a JSON string. 47static JSVM_Value JsonParseNumber(JSVM_Env env, JSVM_CallbackInfo info) 48{ 49 // Set the JSON numeric string to be parsed. 50 std::string strNumber = "10.555"; 51 JSVM_Value jsonString; 52 OH_JSVM_CreateStringUtf8(env, strNumber.c_str(), strNumber.size(), &jsonString); 53 // Call OH_JSVM_JsonParse to parse the JSON string and store the parsed value in result. 54 JSVM_Value result; 55 JSVM_Status status = OH_JSVM_JsonParse(env, jsonString, &result); 56 if (status != JSVM_OK) { 57 OH_LOG_ERROR(LOG_APP, "JSVM JsonParseNumber fail"); 58 } else { 59 OH_LOG_INFO(LOG_APP, "JSVM JsonParseNumber success"); 60 } 61 return result; 62} 63// Parse the object in a JSON string. 64static JSVM_Value JsonParseObject(JSVM_Env env, JSVM_CallbackInfo info) 65{ 66 // Set the JSON object string to be parsed. 67 std::string strObject = "{\"first\": \"one\", \"second\": \"two\", \"third\": \"three\"}"; 68 JSVM_Value strJson; 69 OH_JSVM_CreateStringUtf8(env, strObject.c_str(), strObject.size(), &strJson); 70 // Call OH_JSVM_JsonParse to parse the JSON string and store the parsed value in ret. 71 JSVM_Value ret; 72 JSVM_Status status = OH_JSVM_JsonParse(env, strJson, &ret); 73 if (status != JSVM_OK) { 74 OH_LOG_ERROR(LOG_APP, "JSVM JsonParseObject fail"); 75 } else { 76 OH_LOG_INFO(LOG_APP, "JSVM JsonParseObject success"); 77 } 78 return ret; 79} 80``` 81 82ArkTS code: 83 84```ts 85import hilog from "@ohos.hilog" 86// Import the native APIs. 87import napitest from "libentry.so" 88let script: string = ` 89 jsonParseNumber() 90` 91let script1: string = ` 92 jsonParseObject() 93` 94try { 95 let result = napitest.runJsVm(script); 96 hilog.info(0x0000, 'testJSVM', 'Test JSVM jsonParseNumber: %{public}s', result); 97 let result1 = napitest.runJsVm(script1); 98 hilog.info(0x0000, 'testJSVM', 'Test JSVM jsonParseObject: %{public}s', result1); 99} catch (error) { 100 hilog.error(0x0000, 'testJSVM', 'Test JSVM JsonParse error: %{public}s', error.message); 101} 102``` 103 104### OH_JSVM_JsonStringify 105 106User **OH_JSVM_JsonStringify** to convert an object to a string and return the string value. 107 108CPP code: 109 110```cpp 111// hello.cpp 112#include "napi/native_api.h" 113#include "ark_runtime/jsvm.h" 114#include <hilog/log.h> 115// Register the JsonStringify callback. 116static JSVM_CallbackStruct param[] = { 117 {.data = nullptr, .callback = JsonStringify}, 118}; 119static JSVM_CallbackStruct *method = param; 120// Set a property descriptor named jsonStringify and associate it with a callback. This allows the JsonStringify callback to be called from JS. 121static JSVM_PropertyDescriptor descriptor[] = { 122 {"jsonStringify", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 123}; 124// Define OH_JSVM_JsonStringify. 125static JSVM_Value JsonStringify(JSVM_Env env, JSVM_CallbackInfo info) 126{ 127 // Create an object and convert it to a string. 128 JSVM_Value jsonObject; 129 OH_JSVM_CreateObject(env, &jsonObject); 130 // Set properties. 131 std::string strValue = "JsonStringify"; 132 JSVM_Value value = nullptr; 133 JSVM_Value key; 134 OH_JSVM_CreateStringUtf8(env, "property", JSVM_AUTO_LENGTH, &key); 135 OH_JSVM_CreateStringUtf8(env, strValue.c_str(), strValue.size(), &value); 136 OH_JSVM_SetProperty(env, jsonObject, key, value); 137 // Call OH_JSVM_JsonStringify to convert the object into a string and output the string. 138 JSVM_Value result; 139 JSVM_Status status = OH_JSVM_JsonStringify(env, jsonObject, &result); 140 if (status != JSVM_OK) { 141 OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_JsonStringify fail"); 142 } else { 143 OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_JsonStringify success"); 144 } 145 return result; 146} 147``` 148 149ArkTS code: 150 151```ts 152import hilog from "@ohos.hilog" 153// Import the native APIs. 154import napitest from "libentry.so" 155let script: string = ` 156 jsonStringify() 157` 158try { 159 let result = napitest.runJsVm(script); 160 hilog.info(0x0000, 'testJSVM', 'Test JSVM jsonStringify: %{public}s', result); 161} catch (error) { 162 hilog.error(0x0000, 'testJSVM', 'Test JSVM getVMInfo error: %{public}s', error.message); 163} 164``` 165