• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Performing JSON Operations Using JSVM-API
2
3## Introduction
4
5This topic walks you through on how to use JSVM-API to manipulate data in JavaScript Object Notation (JSON).
6
7## Basic Concepts
8
9JSON: a common text format that is language-independent and can be easily transmitted and stored between the frontend and backend. It is widely used for data processing in JavaScript (JS).
10
11## Available APIs
12
13| API                      | Description                      |
14|----------------------------|--------------------------------|
15| OH_JSVM_JsonParse          | Parses a JSON string and stores the result in a JSON object.|
16| OH_JSVM_JsonStringify      | Converts a JS object into a JSON string stores the result in a JSVM string object.|
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++ code involved in processing JSON data.
21
22### OH_JSVM_JsonParse and OH_JSVM_JsonStringify
23
24Parse a JSON object and return a valid value.
25
26CPP code:
27
28```cpp
29// hello.cpp
30#include <string>
31
32// JS code to be executed.
33static const char *STR_TASK = R"JS(jsonParseNumber();jsonParseObject();)JS";
34
35// Parse a JSON number.
36static JSVM_Value JsonParseNumber(JSVM_Env env, JSVM_CallbackInfo info)
37{
38    // Set the JSON number to be parsed.
39    std::string strNumber = "10.555";
40    JSVM_Value jsonString;
41    JSVM_CALL(OH_JSVM_CreateStringUtf8(env, strNumber.c_str(), strNumber.size(), &jsonString));
42    JSVM_Value jsonObject;
43    // Call OH_JSVM_JsonParse to parse the JSON number and store the result in a JSON object.
44    JSVM_CALL(OH_JSVM_JsonParse(env, jsonString, &jsonObject));
45    double number;
46    JSVM_CALL(OH_JSVM_GetValueDouble(env, jsonObject, &number));
47    OH_LOG_INFO(LOG_APP, "Test JSVM jsonParseNumber: %{public}f", number);
48    return nullptr;
49}
50
51// Parse a JSON object.
52static JSVM_Value JsonParseObject(JSVM_Env env, JSVM_CallbackInfo info)
53{
54    // Set the JSON object string to be parsed.
55    std::string strObject = "{\"first\": \"one\", \"second\": \"two\", \"third\": \"three\"}";
56    JSVM_Value strJson;
57    JSVM_CALL(OH_JSVM_CreateStringUtf8(env, strObject.c_str(), strObject.size(), &strJson));
58    JSVM_Value jsonObject;
59    // Call OH_JSVM_JsonParse to parse the JSON string object and store the result in a JSON object.
60    JSVM_CALL(OH_JSVM_JsonParse(env, strJson, &jsonObject));
61    JSVM_Value jsonString;
62    // Call OH_JSVM_JsonStringify to convert the object into a string and store the string in a JSVM string object.
63    JSVM_CALL(OH_JSVM_JsonStringify(env, jsonObject, &jsonString));
64    size_t totalLen = 0;
65    JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, nullptr, 0, &totalLen));
66    size_t needLen = totalLen + 1;
67    char* buff = new char[needLen];
68    JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, buff, needLen, &totalLen));
69    OH_LOG_INFO(LOG_APP, "Test JSVM jsonParseNumber: %{public}s", buff);
70    delete[] buff;
71    return nullptr;
72}
73
74// Register JsonParse callbacks.
75static JSVM_CallbackStruct param[] = {
76    {.data = nullptr, .callback = JsonParseNumber},
77    {.data = nullptr, .callback = JsonParseObject},
78};
79
80static JSVM_CallbackStruct *method = param;
81
82JSVM_PropertyDescriptor descriptor[] = {
83    {"jsonParseNumber", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
84    {"jsonParseObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
85};
86
87```
88
89## Expected Result
90
91Test JSVM jsonParseNumber: 10.555000
92
93Test JSVM jsonParseNumber: {"first":"one","second":"two","third":"three"}
94