本帖最后由 夜莫离 于 2025-3-22 21:35 编辑
Json解析工具X64
分享下自己使用的Json工具
之前那个工具莫名其妙被删除就使用C重新写了一个不能生成代码 只能分析json 自己复制路径 精易模块的json解析选择 EJSON 然后放入json解析就行了
点击PATH按钮可以复制路径
变量名 | 类 型 | 静态 | 数组 | 备 注 | Json | 类_json | | | str | 文本型 | | |
Json. 解析 (json文本 )str = Json. 取通用属性 (“features[0]”,真 )调试输出 (“str”, str )返回 (str )
选择 YYJSON 解析json后选择树控件的叶子后点击 PATH按钮 复制路径
[C++] 纯文本查看 复制代码 // 通过 JSON Pointer 获取值
static std::string GetValueByPointer(const char* json, const char* pointer) {
// 解析 JSON 文档
yyjson_doc* doc = yyjson_read(json, strlen(json), 0);
if (!doc) {
std::cerr << "Failed to parse JSON.\n";
return "";
}
// 使用 JSON Pointer 获取值
yyjson_val* val = yyjson_doc_ptr_get(doc, pointer);
if (!val) {
std::cerr << "Path not found: " << pointer << "\n";
yyjson_doc_free(doc);
return "";
}
char buffer[32];
memset(buffer, 0, sizeof(buffer));
//snprintf(buffer, sizeof(buffer), "%f", yyjson_get_real(val));
// 根据类型获取值的字符串
switch (yyjson_get_type(val)) {
case YYJSON_TYPE_NULL:
// JSON null 值
snprintf(buffer, sizeof(buffer), "%s", "null");
break; // 默认跳出
case YYJSON_TYPE_BOOL:
// 布尔类型 true/false
snprintf(buffer, sizeof(buffer), "%s", yyjson_get_bool(val) ? "true" : "false");
break; // 默认跳出
case YYJSON_TYPE_STR:
// 字符串类型直接返回
snprintf(buffer, sizeof(buffer), "%s", yyjson_get_str(val));
break; // 默认跳出
case YYJSON_TYPE_NUM: {
yyjson_subtype subtype = yyjson_get_subtype(val);
if (subtype == YYJSON_SUBTYPE_UINT) {
// 无符号整型
snprintf(buffer, sizeof(buffer), "%llu", yyjson_get_uint(val));
break; // 默认跳出
}
else if (subtype == YYJSON_SUBTYPE_SINT) {
// 有符号整型
snprintf(buffer, sizeof(buffer), "%lld", yyjson_get_sint(val));
break; // 默认跳出
}
else if (subtype == YYJSON_SUBTYPE_REAL) {
// 重新创建一个 JSON 文档以解析原始字符串
yyjson_mut_doc* new_doc = yyjson_mut_doc_new(nullptr); // 创建一个新的空 JSON 文档
if (!new_doc) break; // 创建失败直接返回空字符串
// 创建一个可变值并拷贝原始的 val
yyjson_mut_val* new_val = yyjson_val_mut_copy(new_doc, val);
if (!new_val) {
yyjson_mut_doc_free(new_doc); // 清理资源
break;
}
// 将新值包装到一个最小的 JSON 文档中
yyjson_mut_doc* wrapped_doc = yyjson_mut_doc_new(nullptr);
if (!wrapped_doc) {
yyjson_mut_doc_free(new_doc);
return "";
}
yyjson_mut_doc_set_root(wrapped_doc, new_val); // 设置根节点为新值
// 将可变 JSON 转换为原始 JSON 字符串
const char* raw_json = yyjson_mut_write_opts(wrapped_doc, YYJSON_WRITE_ALLOW_INF_AND_NAN, NULL, NULL, 0);
// 有符号整型
snprintf(buffer, sizeof(buffer), "%s", raw_json);
// 清理资源
yyjson_mut_doc_free(new_doc);
yyjson_mut_doc_free(wrapped_doc);
break;
}
break;
}
default:
break;
}
// 处理获取的值
std::string result(buffer, sizeof(buffer));
// 清理资源
yyjson_doc_free(doc);
return result;
}
static int GetValueByPointer_main() {
const char* json = R"({
"level_1": {
"name": "Level1Object",
"is_active": true,
"count": 10,
"level_2": {
"description": "NestedtoLevel2",
"features": ["Fast", "Scalable", "Secure"],
"level_3": {
"meta": {
"created_at": "2025-03-19T12:00:00Z",
"updated_at": "2025-03-20T15:30:00Z",
"revision": 1
},
"values": [123, -456, 3.14159, 2.71828, 9223372036854775807, -9223372036854775808, true, false, "EndOfArray"],
"level_4": [
{
"type": "Object",
"structure": "ArrayofObjects",
"data": {
"a": 1,
"b": 2.5,
"c": "Hello"
}
},
["NestedPrimitiveArray", 1, 1.1, null, true],
{
"deep_nested_object": {
"key": "Value",
"sub_level": {
"int": 100,
"float": 10.99,
"bool": false,
"deeper_array": ["DeepString", 1.23456789012345, 2147483647]
}
}
}
],
"string_in_level_3": "Hello,Level3",
"flag": false
},
"is_running": false
},
"additional_floats": [1.23, 2.34, 3.456789, 4.5e-10, -0.00005]
},
"level_1_array": [
{"item": 1},
{
"level_2_array_object": {
"nested": [
{
"deep_nested": [
{
"even_deeper": {
"key": "ValueatLevel5",
"numbers": [1, 2, 3, 4.5],
"statuses": [true, false]
}
}
]
}
]
}
}
]
})";
const char* path = "/level_1/level_2/level_3/level_4/2/deep_nested_object/sub_level/deeper_array/2";
std::string value = GetValueByPointer(json, path);
std::cout << "Value at path " << path << ": " << value << '\n';
return 0;
}
已修复编辑框长度限制问题
解压密码回复可见
CJsonParser2.zip
(2.01 MB, 下载次数: 8, 售价: 3 枚 精币)
|