主题
获取JSON对象中的字符串值 - JsonGetString
函数简介
获取JSON对象中指定键对应的字符串值。
接口名称
JsonGetStringDLL调用
c
long JsonGetString(long obj, string key, int* err);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| obj | 长整数型 | JSON对象句柄。 |
| key | 字符串 | 键名。 |
| err | 整数型指针 | 错误码输出参数,可为0。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
const char* jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
int32_t err = 0;
int64_t root = ola.JsonParse(jsonStr, &err);
if (root != 0 && err == 0) {
int32_t getErr = 0;
std::string username = ola.JsonGetString(root, "username", &getErr);
ola.JsonFree(root);
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
string jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
int err;
long root = ola.JsonParse(jsonStr, out err);
if (root != 0 && err == 0)
{
int getErr;
string username = ola.JsonGetString(root, "username", out getErr);
ola.JsonFree(root);
}python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
# 假设 json_str 来自 HTTP 响应、文件读取或 MatchImage 等接口
json_str = '{"username":"ola","enabled":true,"vip":false,"level":10}'
root, err = ola.JsonParse(json_str)
if root != 0 and err == 0:
username, get_err = ola.JsonGetString(root, "username")
ola.JsonFree(root)java
import com.olaplug.OLAPlugServer;
import com.sun.jna.ptr.IntByReference;
OLAPlugServer ola = new OLAPlugServer();
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
String jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
IntByReference err = new IntByReference(0);
long root = com.olaplug.OLAPlugDLLHelper.JsonParse(jsonStr, err);
if (root != 0 && err.getValue() == 0) {
com.sun.jna.ptr.IntByReference getErr = new com.sun.jna.ptr.IntByReference(0);
String username = com.olaplug.OLAPlugServer.PtrToStringUTF8(
com.olaplug.OLAPlugDLLHelper.JsonGetString(root, "username", getErr));
ola.JsonFree(root);
}cpp
var ola = com("OlaPlug.OlaSoft")
// 假设 jsonStr 来自 HTTP 响应等接口
var jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}"
var err = 0
var root = ola.JsonParse(jsonStr, err)
if(root && err == 0) {
var getErr = 0
var username = ola.JsonGetString(root, "username", getErr)
ola.JsonFree(root)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
' 假设 jsonStr 来自 HTTP 响应等接口
jsonStr = "{""username"":""ola"",""enabled"":true,""vip"":false,""level"":10}"
err = 0
root = ola.JsonParse(jsonStr, err)
If root <> 0 And err = 0 Then
getErr = 0
username = ola.JsonGetString(root, "username", getErr)
ola.JsonFree(root)
End Iftext
.局部变量 ola, OLAPlug
ola.创建 ()
' 假设 jsonStr 来自 HTTP 响应等接口
jsonStr = “{“username“:“ola“,“enabled“:true,“vip“:false,“level“:10}“
err = 0
root = ola.JsonParse (jsonStr, err)
.如果真 (root ≠ 0 且 err = 0)
getErr = 0
username = ola.JsonGetString (root, "username", getErr)
ola.JsonFree (root)
.如果真结束aardio
import OLAPlugServer;
var ola = OLAPlugServer();
// 假设 jsonStr 来自 HTTP 响应等接口
var jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
var err = 0;
var root = ola.JsonParse(jsonStr, err);
if(root && !err){
var getErr = 0;
var username = ola.JsonGetString(root, "username", getErr);
ola.JsonFree(root);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
// 假设 jsonStr 来自 HTTP 响应等接口
文本型 jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}"
整数 err = 0
长整数 root = ola.JsonParse(jsonStr, err)
如果真 (root ≠ 0 且 err = 0)
{
整数 getErr = 0
文本型 username = ola.JsonGetString(root, "username", getErr)
ola.JsonFree(root)
}cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// 假设 jsonStr 来自 HTTP 响应、文件读取或 MatchImage 等接口
const char* jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
int32_t err = 0;
int64_t root = ola.JsonParse(jsonStr, &err);
if (root != 0 && err == 0) {
int32_t getErr = 0;
std::string username = ola.JsonGetString(root, "username", &getErr);
ola.JsonFree(root);
}原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
const char* jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
int err = 0;
long root = JsonParse(instance, jsonStr, &err);
if (root != 0 && err == 0) {
long instance = CreateCOLAPlugInterFace();
const char* jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
int err = 0;
long root = JsonParse(instance, jsonStr, &err);
if (root != 0) {
long ptr = JsonGetString(instance, root, "username", 0);
if (ptr != 0) {
char username[256] = {0};
GetStringFromPtr(ptr, username, sizeof(username));
FreeStringPtr(ptr);
}
JsonFree(instance, root);
}
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 = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
int err = 0;
long root = JsonParse(instance, jsonStr, err);
if (root != 0 && err == 0)
{
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long JsonGetString(long ola, long obj, string key, int err);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long JsonParse(long ola, string str, int err);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int JsonFree(long ola, long obj);
long instance = CreateCOLAPlugInterFace();
string jsonStr = "{\"username\":\"ola\",\"enabled\":true,\"vip\":false,\"level\":10}";
long root = JsonParse(instance, jsonStr, 0);
if (root != 0) {
long ptr = JsonGetString(instance, root, "username", 0);
if (ptr != 0) {
StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
FreeStringPtr(ptr);
}
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'{"username":"ola","enabled":true,"vip":false,"level":10}'
err = c_int(0)
root = ola.JsonParse(instance, json_str, err)
if root and err.value == 0:
get_err = c_int(0)
ptr = ola.JsonGetString(instance, root, b"username", get_err)
if ptr:
buf = create_string_buffer(256)
ola.GetStringFromPtr(ptr, buf, 256)
ola.FreeStringPtr(ptr)
username = buf.value.decode("utf-8")
ola.JsonFree(instance, root)返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 返回字符串指针,失败时返回0。需调用 FreeStringPtr 释放内存。 |
注意事项
| 项目 | 说明 |
|---|---|
| 释放内存 | 返回的字符串需要调用 FreeStringPtr 释放内存。 |
| 如果键不存在或值不是字符串类型 | 如果键不存在或值不是字符串类型,返回0。 |
