Skip to content

获取JSON数组中的元素 - JsonGetArrayItem

函数简介

获取JSON数组中指定索引位置的元素,返回JSON值句柄。

接口名称

JsonGetArrayItem

DLL调用

c
long JsonGetArrayItem(long arr, int index, int* err);

参数说明

参数名类型说明
arr长整数型JSON数组句柄。
index整数型元素索引(从0开始)。
err整数型指针错误码输出参数,可为0。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
const char* jsonStr = "[\"apple\",\"banana\",\"cherry\"]";
int32_t err = 0;
int64_t arr = ola.JsonParse(jsonStr, &err);
if (arr != 0 && err == 0) {
    int32_t getErr = 0;
    int64_t item = ola.JsonGetArrayItem(arr, 1, &getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree(arr);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
string jsonStr = "[\"apple\",\"banana\",\"cherry\"]";
int err;
long arr = ola.JsonParse(jsonStr, out err);
if (arr != 0 && err == 0)
{
    int getErr;
    long item = ola.JsonGetArrayItem(arr, 1, out getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree(arr);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
# 假设 json_str 来自 HTTP 响应、文件读取或 MatchImage 等接口
json_str = '["apple","banana","cherry"]'
arr, err = ola.JsonParse(json_str)
if arr != 0 and err == 0:
    item, get_err = ola.JsonGetArrayItem(arr, 1)  # "banana"
    if item != 0:
        ola.JsonFree(item)
    ola.JsonFree(arr)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
String jsonStr = "[\"apple\",\"banana\",\"cherry\"]";
int err;
long arr = ola.JsonParse(jsonStr, err);
if (arr != 0 && err == 0) {
    int32_t getErr = 0;
    int64_t item = ola.JsonGetArrayItem(arr, 1, &getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree(arr);
}
cpp
var ola = com("OlaPlug.OlaSoft")
// 假设 jsonStr 来自 HTTP 响应等接口
var jsonStr = "[\"apple\",\"banana\",\"cherry\"]"
var err = 0
var arr = ola.JsonParse(jsonStr, err)
if(arr && err == 0) {
    int32_t getErr = 0;
    int64_t item = ola.JsonGetArrayItem(arr, 1, &getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree(arr)
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
' 假设 jsonStr 来自 HTTP 响应等接口
jsonStr = "[""apple"",""banana"",""cherry""]"
err = 0
arr = ola.JsonParse(jsonStr, err)
If arr <> 0 And err = 0 Then
    int32_t getErr = 0;
    int64_t item = ola.JsonGetArrayItem(arr, 1, &getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree(arr)
End If
text
.局部变量 ola, OLAPlug
ola.创建 ()
' 假设 jsonStr 来自 HTTP 响应等接口
jsonStr = “[“apple“,“banana“,“cherry“]“
err = 0
arr = ola.JsonParse (jsonStr, err)
.如果真 (arr ≠ 0 且 err = 0)
    int32_t getErr = 0;
    int64_t item = ola.JsonGetArrayItem(arr, 1, &getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree (arr)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
// 假设 jsonStr 来自 HTTP 响应等接口
var jsonStr = "[\"apple\",\"banana\",\"cherry\"]";
var err = 0;
var arr = ola.JsonParse(jsonStr, err);
if(arr && !err){
    int32_t getErr = 0;
    int64_t item = ola.JsonGetArrayItem(arr, 1, &getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree(arr);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
// 假设 jsonStr 来自 HTTP 响应等接口
文本型 jsonStr = "[\"apple\",\"banana\",\"cherry\"]"
整数 err = 0
长整数 arr = ola.JsonParse(jsonStr, err)
如果真 (arr ≠ 0 且 err = 0)
{
    int32_t getErr = 0;
    int64_t item = ola.JsonGetArrayItem(arr, 1, &getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree(arr)
}
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
const char* jsonStr = "[\"apple\",\"banana\",\"cherry\"]";
int32_t err = 0;
int64_t arr = ola.JsonParse(jsonStr, &err);
if (arr != 0 && err == 0) {
    int32_t getErr = 0;
    int64_t item = ola.JsonGetArrayItem(arr, 1, &getErr); // "banana"
    if (item != 0)
        ola.JsonFree(item);
    ola.JsonFree(arr);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
const char* jsonStr = "[\"apple\",\"banana\",\"cherry\"]";
int err = 0;
long arr = JsonParse(instance, jsonStr, &err);
if (arr != 0 && err == 0) {
    long item = JsonGetArrayItem(instance, arr, 1, 0);
    if (item != 0)
        JsonFree(instance, item);
    JsonFree(instance, arr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
string jsonStr = "[\"apple\",\"banana\",\"cherry\"]";
int err = 0;
long arr = JsonParse(instance, jsonStr, err);
if (arr != 0 && err == 0)
{
    long item = JsonGetArrayItem(instance, arr, 1, 0);
    if (item != 0)
        JsonFree(instance, item);
    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()
json_str = b'["apple","banana","cherry"]'
err = c_int(0)
arr = ola.JsonParse(instance, json_str, err)
if arr and err.value == 0:
    long item = JsonGetArrayItem(instance, arr, 1, 0);
    if (item != 0)
        JsonFree(instance, item);
    ola.JsonFree(instance, arr)

返回值

返回值说明
(返回值)返回数组元素句柄,失败时返回0。错误码通过 err 参数返回。

注意事项

项目说明
释放内存返回的元素句柄需要调用 JsonFree 释放内存。
索引从0开始计数索引从0开始计数。