Skip to content

获取JSON对象中的值 - JsonGetValue

函数简介

获取JSON对象中指定键对应的值,返回JSON值句柄。

接口名称

JsonGetValue

DLL调用

c
long JsonGetValue(long obj, string key, int* err);

参数说明

参数名类型说明
obj长整数型JSON对象句柄。
key字符串键名。
err整数型指针错误码输出参数,可为0。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
const char* jsonStr = "{\"users\":[{\"name\":\"alice\",\"active\":true},{\"name\":\"bob\",\"active\":false}]}";
int32_t err = 0;
int64_t root = ola.JsonParse(jsonStr, &err);
if (root != 0 && err == 0) {
    int32_t getErr = 0;
    int64_t users = ola.JsonGetValue(root, "users", &getErr);
    if (users != 0) {
        int32_t itemErr = 0;
        int64_t first = ola.JsonGetArrayItem(users, 0, &itemErr);
        ola.JsonFree(first);
        ola.JsonFree(users);
    }
    ola.JsonFree(root);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
string jsonStr = "{\"users\":[{\"name\":\"alice\",\"active\":true},{\"name\":\"bob\",\"active\":false}]}";
int err;
long root = ola.JsonParse(jsonStr, out err);
if (root != 0 && err == 0)
{
    int getErr;
    long users = ola.JsonGetValue(root, "users", out getErr);
    if (users != 0)
    {
        int itemErr;
        long first = ola.JsonGetArrayItem(users, 0, out itemErr);
        ola.JsonFree(first);
        ola.JsonFree(users);
    }
    ola.JsonFree(root);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
# 假设 json_str 来自 HTTP 响应、文件读取或 MatchImage 等接口
json_str = '{"users":[{"name":"alice","active":true},{"name":"bob","active":false}]}'
root, err = ola.JsonParse(json_str)
if root != 0 and err == 0:
    users, get_err = ola.JsonGetValue(root, "users")
    if users != 0:
        first, _ = ola.JsonGetArrayItem(users, 0)
        ola.JsonFree(users)
    ola.JsonFree(root)
java
import com.olaplug.OLAPlugServer;

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

OLAPlugServer ola;
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
const char* jsonStr = "{\"users\":[{\"name\":\"alice\",\"active\":true},{\"name\":\"bob\",\"active\":false}]}";
int32_t err = 0;
int64_t root = ola.JsonParse(jsonStr, &err);
if (root != 0 && err == 0) {
    int32_t getErr = 0;
    int64_t users = ola.JsonGetValue(root, "users", &getErr);
    if (users != 0) {
        int32_t itemErr = 0;
        int64_t first = ola.JsonGetArrayItem(users, 0, &itemErr);
        ola.JsonFree(first);
        ola.JsonFree(users);
    }
    ola.JsonFree(root);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
const char* jsonStr = "{\"users\":[{\"name\":\"alice\",\"active\":true},{\"name\":\"bob\",\"active\":false}]}";
int err = 0;
long root = JsonParse(instance, jsonStr, &err);
if (root != 0 && err == 0) {
    long users = JsonGetValue(instance, root, "users", 0);
    if (users != 0) {
        long first = JsonGetArrayItem(instance, users, 0, 0);
        JsonFree(instance, first);
        JsonFree(instance, users);
    }
    JsonFree(instance, root);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

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

long instance = CreateCOLAPlugInterFace();
string jsonStr = "{\"users\":[{\"name\":\"alice\",\"active\":true},{\"name\":\"bob\",\"active\":false}]}";
int err = 0;
long root = JsonParse(instance, jsonStr, err);
if (root != 0 && err == 0)
{
    long users = JsonGetValue(instance, root, "users", 0);
    if (users != 0) {
        long first = JsonGetArrayItem(instance, users, 0, 0);
        JsonFree(instance, first);
        JsonFree(instance, users);
    }
    JsonFree(instance, root);
}
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'{"users":[{"name":"alice","active":true},{"name":"bob","active":false}]}'
err = c_int(0)
root = ola.JsonParse(instance, json_str, err)
if root and err.value == 0:
    long users = JsonGetValue(instance, root, "users", 0);
    if (users != 0) {
        long first = JsonGetArrayItem(instance, users, 0, 0);
        JsonFree(instance, first);
        JsonFree(instance, users);
    }
    ola.JsonFree(instance, root)

返回值

返回值说明
(返回值)返回对应的JSON值句柄,失败时返回0。错误码通过 err 参数返回。

注意事项

项目说明
释放内存返回的值句柄需要调用 JsonFree 释放内存。