您的位置:首页 > 财经 > 金融 > jsmn输出

jsmn输出

2024/12/23 3:23:02 来源:https://blog.csdn.net/zisehuoxia/article/details/141791863  浏览:    关键词:jsmn输出

对应c程序:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsmn.h"#define my_printf(format, ...) printf(format, ##__VA_ARGS__)
//#define my_printf(format, ...)
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
const char * get_jsmn_token_type(const jsmntype_t type)
{switch (type) {case JSMN_UNDEFINED: return "JSMN_UNDEFINED";case JSMN_OBJECT: return "JSMN_OBJECT";case JSMN_ARRAY: return "JSMN_ARRAY";case JSMN_STRING: return "JSMN_STRING";case JSMN_PRIMITIVE: return "JSMN_PRIMITIVE";default: return "Unknown"; // 处理未知类型}
}
static int jsoneq(const char *json, jsmntok_t *tok, const char *s)
{if (tok->start >= tok->end){return -1;}if ( tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start &&strncmp(json + tok->start, s, tok->end - tok->start) == 0){return 0;}return -1;
}int main(int argc, char *argv[])
{const char *json = "{\"1\":\"11\",\"data\":{\"2\":22,\"3\":33,\"4\":44,\"5\":55,\"6\":66,\"7\":77,\"8\":88,\"9\":99,\"A\":AA,\"B\":BB,\"C\":CC,\"D\":DD,\"E\":EE,\"F\":FF,\"G\":GG,\"H\":\"HH\"},\"I\":0}";jsmn_parser parser;jsmntok_t tokens[64]; // 假设最多解析x个tokensconst char *value;uint16_t value_len;char tmp[64];// 初始化解析器jsmn_init(&parser);// 解析JSON字符串int num_tokens = jsmn_parse(&parser, json, strlen(json), tokens, sizeof(tokens)/sizeof(tokens[0]));if (num_tokens < 0) {my_printf("Failed to parse JSON: %d\n", num_tokens);return 1;}int i, j, k;for(i = 0;i < num_tokens; i++){my_printf("[line:%3d][%2d][type:%16s][size:%d][%.*s]\n", __LINE__, i, get_jsmn_token_type(tokens[i].type), tokens[i].size, tokens[i].end-tokens[i].start, json+tokens[i].start);}for (i = 0; i < num_tokens; i++){if (tokens[i].type == JSMN_STRING && tokens[i].size == 1){ // name均为JSMN_STRING且size=1my_printf("[line:%3d][%2d][type:%16s][size:%d]key: %.*s ",__LINE__, i, get_jsmn_token_type(tokens[i].type), tokens[i].size, tokens[i].end-tokens[i].start, json+tokens[i].start);if (jsoneq(json, &tokens[i], "data") == 0){ // data内为objectjsmntok_t *data_token = &tokens[i + 1];my_printf("\n[line:%3d][%2d][type:%16s][size:%d]value:%.*s\n",__LINE__, i + 1, get_jsmn_token_type(data_token->type), data_token->size, data_token->end-data_token->start, json+data_token->start);if (data_token->type == JSMN_OBJECT){for (j=i+2, k=0; j<num_tokens && k<data_token->size; j++,k++){if (tokens[j].type == JSMN_STRING && tokens[j].size == 1){my_printf("[line:%3d][%2d][type:%16s][size:%d]key: %.*s ",__LINE__, j, get_jsmn_token_type(tokens[j].type), tokens[j].size, tokens[j].end-tokens[j].start, json+tokens[j].start);// 获取valuevalue = json + tokens[j + 1].start;value_len = tokens[j + 1].end - tokens[j + 1].start;memset (tmp, 0, sizeof(tmp));if (value_len && value_len < sizeof(tmp)){memcpy (tmp, value, value_len);my_printf("[line:%3d]value:%.*s [size:%d][type:%16s]\n", __LINE__, value_len, value, tokens[j+1].size, get_jsmn_token_type(tokens[j+1].type));}j ++;}}i = j-1;}}else{value = json + tokens[i + 1].start;value_len = tokens[i + 1].end - tokens[i + 1].start;my_printf("[line:%3d]value:%.*s [size:%d][type:%16s]\n", __LINE__, value_len, value, tokens[i+1].size, get_jsmn_token_type(tokens[i+1].type));i++;}}}return 0;
}

.vscode目录下:

c_cpp_properties.json内容:

{"configurations": [{"name": "Win32","includePath": ["${workspaceFolder}/**"],"defines": ["_DEBUG","UNICODE","_UNICODE"],"cStandard": "c17","cppStandard": "c++17","compilerPath": "E:\\codeblocks-20.03mingw-nosetup\\MinGW\\bin\\g++.exe"}],"version": 4
}

tasks.json内容:
 

{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "编译jsmn项目文件","command": "E:\\codeblocks-20.03mingw-nosetup\\MinGW\\bin\\g++.exe","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {"cwd": "E:\\codeblocks-20.03mingw-nosetup\\MinGW\\bin"},"problemMatcher": ["$gcc"],"group": "build","detail": "编译器: E:\\codeblocks-20.03mingw-nosetup\\MinGW\\bin\\g++.exe"}]
}

launch.json内容:

{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "E:\\codeblocks-20.03mingw-nosetup\\MinGW\\bin\\gdb.exe","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "将反汇编风格设置为 Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}]}]
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com