1# 使用JSVM-API接口进行JSON操作 2<!--Kit: NDK Development--> 3<!--Subsystem: arkcompiler--> 4<!--Owner: @yuanxiaogou; @string_sz--> 5<!--Designer: @knightaoko--> 6<!--Tester: @test_lzz--> 7<!--Adviser: @fang-jinxu--> 8 9## 简介 10 11使用JSVM-API接口操作JSON数据时,JSVM模块中的相关接口可以直接处理JSON格式的数据。 12 13## 基本概念 14 15- **JSON(JavaScript Object Notation)**:是一种常见的数据交换格式,用于前后端数据的传递、存储和交流。可以与多种编程语言进行交互,在JavaScript中被广泛应用于数据处理。 16 17## 接口说明 18 19| 接口 | 功能说明 | 20|----------------------------|--------------------------------| 21| OH_JSVM_JsonParse | 解析JSON字符串,并将结果存储在JSON对象。 | 22| OH_JSVM_JsonStringify | 将对象字符串化,并将结果存储在JSVM字符串对象。 | 23 24## 使用示例 25 26JSVM-API接口开发流程参考[使用JSVM-API实现JS与C/C++语言交互开发流程](use-jsvm-process.md),本文仅展示接口对应的C++相关代码。 27 28### OH_JSVM_JsonParse & OH_JSVM_JsonStringify 29 30解析JSON对象,并输出有效的解析结果。 31 32cpp部分代码: 33 34```cpp 35// hello.cpp 36#include <string> 37 38// 解析JSON数字 39static JSVM_Value JsonParseNumber(JSVM_Env env, JSVM_CallbackInfo info) 40{ 41 // 设置要解析的JSON数字 42 std::string strNumber = "10.555"; 43 JSVM_Value jsonString = nullptr; 44 JSVM_CALL(OH_JSVM_CreateStringUtf8(env, strNumber.c_str(), strNumber.size(), &jsonString)); 45 JSVM_Value jsonObject = nullptr; 46 // 调用OH_JSVM_JsonParse函数解析JSON数字,并将结果存储在JSON对象中 47 JSVM_CALL(OH_JSVM_JsonParse(env, jsonString, &jsonObject)); 48 double number = 0.0f; 49 JSVM_CALL(OH_JSVM_GetValueDouble(env, jsonObject, &number)); 50 OH_LOG_INFO(LOG_APP, "Test JSVM jsonParseNumber: %{public}f", number); 51 return nullptr; 52} 53 54// 解析JSON字符串中的对象 55static JSVM_Value JsonParseObject(JSVM_Env env, JSVM_CallbackInfo info) 56{ 57 // 设置要解析的JSON对象字符串 58 std::string strObject = "{\"first\": \"one\", \"second\": \"two\", \"third\": \"three\"}"; 59 JSVM_Value strJson = nullptr; 60 JSVM_CALL(OH_JSVM_CreateStringUtf8(env, strObject.c_str(), strObject.size(), &strJson)); 61 JSVM_Value jsonObject = nullptr; 62 // 调用OH_JSVM_JsonParse函数解析JSON对象字符串,并将结果存储在JSON对象中 63 JSVM_CALL(OH_JSVM_JsonParse(env, strJson, &jsonObject)); 64 JSVM_Value jsonString = nullptr; 65 // 调用OH_JSVM_JsonStringify函数将对象转换为字符串格式,并将结果存储在JSVM字符串对象中 66 JSVM_CALL(OH_JSVM_JsonStringify(env, jsonObject, &jsonString)); 67 size_t totalLen = 0; 68 JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, nullptr, 0, &totalLen)); 69 size_t needLen = totalLen + 1; 70 char* buff = new char[needLen]; 71 JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, buff, needLen, &totalLen)); 72 OH_LOG_INFO(LOG_APP, "Test JSVM jsonParseObject: %{public}s", buff); 73 delete[] buff; 74 return nullptr; 75} 76 77// JsonParse注册回调 78static JSVM_CallbackStruct param[] = { 79 {.data = nullptr, .callback = JsonParseNumber}, 80 {.data = nullptr, .callback = JsonParseObject}, 81}; 82 83static JSVM_CallbackStruct *method = param; 84 85JSVM_PropertyDescriptor descriptor[] = { 86 {"jsonParseNumber", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 87 {"jsonParseObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 88}; 89 90// 待执行的js代码 91static const char *srcCallNative = R"JS(jsonParseNumber();jsonParseObject();)JS"; 92``` 93<!-- @[oh_jsvm_json_parse_and_json_stringify](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/hello.cpp) --> 94 95## 预期结果: 96```cpp 97Test JSVM jsonParseNumber: 10.555000 98 99Test JSVM jsonParseObject: {"first":"one","second":"two","third":"three"} 100``` 101