1# 使用JSVM-API接口进行JSON操作 2 3## 简介 4 5JSON格式的数据可用于实现前后端之间的数据传递、存储和交流,并且可以与多种编程语言进行交互。 6 7## 基本概念 8 9- **JSON(JavaScript Object Notation)**:是一种常见的数据交换格式,在JavaScript中被广泛应用于数据处理。 10 11## 接口说明 12 13| 接口 | 功能说明 | 14|----------------------------|--------------------------------| 15| OH_JSVM_JsonParse | 解析JSON字符串,并返回成功解析的值。 | 16| OH_JSVM_JsonStringify | 将对象字符串化,并返回成功转换后的字符串。 | 17 18## 使用示例 19 20JSVM-API接口开发流程参考[使用JSVM-API实现JS与C/C++语言交互开发流程](use-jsvm-process.md),本文仅对接口对应C++及ArkTS相关代码进行展示。 21 22### OH_JSVM_JsonParse 23 24对JSON字符串执行解析操作,并返回解析结果的有效值。 25 26cpp部分代码 27 28```cpp 29// hello.cpp 30#include "napi/native_api.h" 31#include "ark_runtime/jsvm.h" 32#include <hilog/log.h> 33// JsonParse注册回调 34static JSVM_CallbackStruct param[] = { 35 {.data = nullptr, .callback = JsonParseNumber}, 36 {.data = nullptr, .callback = JsonParseObject}, 37}; 38static JSVM_CallbackStruct *method = param; 39// JsonParse方法别名,供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// OH_JSVM_JsonParse的样例方法 45 46// 解析JSON字符串中的数字 47static JSVM_Value JsonParseNumber(JSVM_Env env, JSVM_CallbackInfo info) 48{ 49 // 设置要解析的JSON数字字符串 50 std::string strNumber = "10.555"; 51 JSVM_Value jsonString; 52 OH_JSVM_CreateStringUtf8(env, strNumber.c_str(), strNumber.size(), &jsonString); 53 // 调用OH_JSVM_JsonParse函数解析JSON字符串,并将结果存储在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// 解析JSON字符串中的对象 64static JSVM_Value JsonParseObject(JSVM_Env env, JSVM_CallbackInfo info) 65{ 66 // 设置要解析的JSON对象字符串 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 // 调用OH_JSVM_JsonParse函数解析JSON字符串,并将结果存储在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侧示例代码 83 84```ts 85import hilog from "@ohos.hilog" 86// 通过import的方式,引入Native能力。 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 106将对象转换为字符串格式,并返回转换成功的字符串值。 107 108cpp部分代码 109 110```cpp 111// hello.cpp 112#include "napi/native_api.h" 113#include "ark_runtime/jsvm.h" 114#include <hilog/log.h> 115// JsonStringify注册回调 116static JSVM_CallbackStruct param[] = { 117 {.data = nullptr, .callback = JsonStringify}, 118}; 119static JSVM_CallbackStruct *method = param; 120// JsonStringify方法别名,供JS调用 121static JSVM_PropertyDescriptor descriptor[] = { 122 {"jsonStringify", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 123}; 124// OH_JSVM_JsonStringify的样例方法 125static JSVM_Value JsonStringify(JSVM_Env env, JSVM_CallbackInfo info) 126{ 127 // 创建一个对象并将其转换成字符串 128 JSVM_Value jsonObject; 129 OH_JSVM_CreateObject(env, &jsonObject); 130 // 设置属性 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 // 调用OH_JSVM_JsonStringify接口将object对象转换成字符串并输出 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侧示例代码 150 151```ts 152import hilog from "@ohos.hilog" 153// 通过import的方式,引入Native能力。 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