目录

Performing JSON Operations Using JSVM-API

Introduction

This topic walks you through on how to use JSVM-API to manipulate data in JavaScript Object Notation (JSON).

Basic Concepts

Available APIs

API Description
OH_JSVM_JsonParse Parses a JSON string and stores the result in a JSON object.
OH_JSVM_JsonStringify Stringifies an object and stores the result in a JSVM string object.

Example

If you are just starting out with JSVM-API, see JSVM-API Development Process. The following demonstrates only the C++ code involved in related APIs.

[h2]OH_JSVM_JsonParse & OH_JSVM_JsonStringify

Use OH_JSVM_JsonParse to parse a JSON object and OH_JSVM_JsonStringify to output the parsing result.

CPP code:

// Parse a JSON number.
static JSVM_Value JsonParseNumber(JSVM_Env env, JSVM_CallbackInfo info)
{
    // Set the JSON number to parse.
    std::string strNumber = "10.555";
    JSVM_Value jsonString = nullptr;
    JSVM_CALL(OH_JSVM_CreateStringUtf8(env, strNumber.c_str(), strNumber.size(), &jsonString));
    JSVM_Value jsonObject = nullptr;
    // Call OH_JSVM_JsonParse to parse the JSON number and store the result in a JSON object.
    JSVM_CALL(OH_JSVM_JsonParse(env, jsonString, &jsonObject));
    double number = 0.0f;
    JSVM_CALL(OH_JSVM_GetValueDouble(env, jsonObject, &number));
    OH_LOG_INFO(LOG_APP, "Test JSVM jsonParseNumber: %{public}f", number);
    return nullptr;
}

// Parse an object from a JSON string.
static JSVM_Value JsonParseObject(JSVM_Env env, JSVM_CallbackInfo info)
{
    // Set the JSON object string to parse.
    std::string strObject = "{\"first\": \"one\", \"second\": \"two\", \"third\": \"three\"}";
    JSVM_Value strJson = nullptr;
    JSVM_CALL(OH_JSVM_CreateStringUtf8(env, strObject.c_str(), strObject.size(), &strJson));
    JSVM_Value jsonObject = nullptr;
    // Call OH_JSVM_JsonParse to parse the JSON object string and store the result in a JSON object.
    JSVM_CALL(OH_JSVM_JsonParse(env, strJson, &jsonObject));
    JSVM_Value jsonString = nullptr;
    // Call OH_JSVM_JsonStringify to convert the object to a string and store the result in a JSVM string object.
    JSVM_CALL(OH_JSVM_JsonStringify(env, jsonObject, &jsonString));
    size_t totalLen = 0;
    JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, nullptr, 0, &totalLen));
    size_t needLen = totalLen + 1;
    char* buff = new char[needLen];
    JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, buff, needLen, &totalLen));
    OH_LOG_INFO(LOG_APP, "Test JSVM jsonParseObject: %{public}s", buff);
    delete[] buff;
    return nullptr;
}

// Register the JsonParse callback.
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = JsonParseNumber},
    {.data = nullptr, .callback = JsonParseObject},
};

static JSVM_CallbackStruct *method = param;

static JSVM_PropertyDescriptor descriptor[] = {
    {"jsonParseNumber", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT},
    {"jsonParseObject", nullptr, method + 1, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};

// JS code to execute.
static const char *STR_TASK = R"JS(jsonParseNumber();jsonParseObject();)JS";

[h2]Expected Result

Test JSVM jsonParseNumber: 10.555000

Test JSVM jsonParseObject: {"first":"one","second":"two","third":"three"}