主题
向JSON数组添加元素 - JsonArrayAppend
函数简介
向JSON数组末尾添加元素,支持添加任意类型的JSON值。
接口名称
JsonArrayAppendDLL调用
c
int JsonArrayAppend(long arr, long value);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| arr | 长整数型 | JSON数组句柄。 |
| value | 长整数型 | 要添加的元素句柄。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int64_t arr = ola.JsonCreateArray();
if (arr != 0) {
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
int32_t strErr = 0;
std::string jsonOut = ola.JsonStringify(arr, 0, &strErr);
ola.JsonFree(arr);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
long arr = ola.JsonCreateArray();
if (arr != 0)
{
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
int strErr;
string jsonOut = ola.JsonStringify(arr, 0, out strErr);
ola.JsonFree(arr);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
arr = ola.JsonCreateArray()
if arr != 0:
item, _ = ola.JsonParse('"cherry"')
ret = ola.JsonArrayAppend(arr, item)
json_out, str_err = ola.JsonStringify(arr, 0)
ola.JsonFree(arr)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
long arr = ola.JsonCreateArray();
if (arr != 0) {
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
int strErr;
String jsonOut = ola.JsonStringify(arr, 0, strErr);
ola.JsonFree(arr);
}cpp
var ola = com("OlaPlug.OlaSoft")
var arr = ola.JsonCreateArray()
if(arr) {
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
var strErr = 0
var jsonOut = ola.JsonStringify(arr, 0, strErr)
ola.JsonFree(arr)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
arr = ola.JsonCreateArray()
If arr <> 0 Then
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
strErr = 0
jsonOut = ola.JsonStringify(arr, 0, strErr)
ola.JsonFree(arr)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
arr = ola.JsonCreateArray ()
.如果真 (arr ≠ 0)
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
strErr = 0
jsonOut = ola.JsonStringify (arr, 0, strErr)
ola.JsonFree (arr)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var arr = ola.JsonCreateArray();
if(arr){
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
var strErr = 0;
var jsonOut = ola.JsonStringify(arr, 0, strErr);
ola.JsonFree(arr);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 arr = ola.JsonCreateArray()
如果真 (arr ≠ 0)
{
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
整数 strErr = 0
文本型 jsonOut = ola.JsonStringify(arr, 0, strErr)
ola.JsonFree(arr)
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int64_t arr = ola.JsonCreateArray();
if (arr != 0) {
int32_t perr = 0;
int64_t item = ola.JsonParse("\"cherry\"", &perr);
int ret = ola.JsonArrayAppend(arr, item);
int32_t strErr = 0;
std::string jsonOut = ola.JsonStringify(arr, 0, &strErr);
ola.JsonFree(arr);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long arr = JsonCreateArray(instance);
if (arr != 0) {
int err = 0;
long item = JsonParse(instance, "\"apple\"", &err);
JsonArrayAppend(instance, arr, item);
long ptr = JsonStringify(instance, arr, 0, 0);
if (ptr != 0) {
char buffer[512] = {0};
GetStringFromPtr(ptr, buffer, sizeof(buffer));
FreeStringPtr(ptr);
}
JsonFree(instance, arr);
}csharp
long instance = CreateCOLAPlugInterFace();
long arr = JsonCreateArray(instance);
if (arr != 0) {
int err = 0;
long item = JsonParse(instance, "\"apple\"", &err);
JsonArrayAppend(instance, arr, item);
long ptr = JsonStringify(instance, arr, 0, 0);
if (ptr != 0) {
char buffer[512] = {0};
GetStringFromPtr(ptr, buffer, sizeof(buffer));
FreeStringPtr(ptr);
}
JsonFree(instance, arr);
}python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
arr = ola.JsonCreateArray(instance)
if arr:
err = c_int(0)
item = ola.JsonParse(instance, b'"apple"', err)
ola.JsonArrayAppend(instance, arr, item)
ptr = ola.JsonStringify(instance, arr, 0, err)
if ptr:
buf = create_string_buffer(512)
ola.GetStringFromPtr(ptr, buf, 512)
ola.FreeStringPtr(ptr)
ola.JsonFree(instance, arr)返回值
| 返回值 | 说明 |
|---|---|
0 | 成功。 |
| 其他 | 错误码,失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| 添加成功后 | 添加成功后,value 句柄的所有权转移给 arr,不需要单独释放。 |
| 元素会被添加到数组末尾 | 元素会被添加到数组末尾。 |
| 支持添加对象、数组、字符串、数字等任意JSON类 | 支持添加对象、数组、字符串、数字等任意JSON类型。 |
