jsmn输出

对应c程序:


#include 
#include 
#include 
#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个tokens
    const 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=1
            my_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内为object
                jsmntok_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; jsize; 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);
                            // 获取value
                            value = 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;
}

输出结果如下,可以看出类型的不同(带引号和不带引号区别,OBJECT)

[line: 60][ 0][type:     JSMN_OBJECT][size:3][{"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}]
[line: 60][ 1][type:     JSMN_STRING][size:1][1]
[line: 60][ 2][type:     JSMN_STRING][size:0][11]
[line: 60][ 3][type:     JSMN_STRING][size:1][data]
[line: 60][ 4][type:     JSMN_OBJECT][size:16][{"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"}]
[line: 60][ 5][type:     JSMN_STRING][size:1][2]
[line: 60][ 6][type:  JSMN_PRIMITIVE][size:0][22]
[line: 60][ 7][type:     JSMN_STRING][size:1][3]
[line: 60][ 8][type:  JSMN_PRIMITIVE][size:0][33]
[line: 60][ 9][type:     JSMN_STRING][size:1][4]
[line: 60][10][type:  JSMN_PRIMITIVE][size:0][44]
[line: 60][11][type:     JSMN_STRING][size:1][5]
[line: 60][12][type:  JSMN_PRIMITIVE][size:0][55]
[line: 60][13][type:     JSMN_STRING][size:1][6]
[line: 60][14][type:  JSMN_PRIMITIVE][size:0][66]
[line: 60][15][type:     JSMN_STRING][size:1][7]
[line: 60][16][type:  JSMN_PRIMITIVE][size:0][77]
[line: 60][17][type:     JSMN_STRING][size:1][8]
[line: 60][18][type:  JSMN_PRIMITIVE][size:0][88]
[line: 60][19][type:     JSMN_STRING][size:1][9]
[line: 60][20][type:  JSMN_PRIMITIVE][size:0][99]
[line: 60][21][type:     JSMN_STRING][size:1][A]
[line: 60][22][type:  JSMN_PRIMITIVE][size:0][AA]
[line: 60][23][type:     JSMN_STRING][size:1][B]
[line: 60][24][type:  JSMN_PRIMITIVE][size:0][BB]
[line: 60][25][type:     JSMN_STRING][size:1][C]
[line: 60][26][type:  JSMN_PRIMITIVE][size:0][CC]
[line: 60][27][type:     JSMN_STRING][size:1][D]
[line: 60][28][type:  JSMN_PRIMITIVE][size:0][DD]
[line: 60][29][type:     JSMN_STRING][size:1][E]
[line: 60][30][type:  JSMN_PRIMITIVE][size:0][EE]
[line: 60][31][type:     JSMN_STRING][size:1][F]
[line: 60][32][type:  JSMN_PRIMITIVE][size:0][FF]
[line: 60][33][type:     JSMN_STRING][size:1][G]
[line: 60][34][type:  JSMN_PRIMITIVE][size:0][GG]
[line: 60][35][type:     JSMN_STRING][size:1][H]
[line: 60][36][type:     JSMN_STRING][size:0][HH]
[line: 60][37][type:     JSMN_STRING][size:1][I]
[line: 60][38][type:  JSMN_PRIMITIVE][size:0][0]
[line: 67][ 1][type:     JSMN_STRING][size:1]key: 1 [line: 99]value:11 [size:0][type:     JSMN_STRING]
[line: 67][ 3][type:     JSMN_STRING][size:1]key: data
[line: 71][ 4][type:     JSMN_OBJECT][size:16]value:{"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"}
[line: 78][ 5][type:     JSMN_STRING][size:1]key: 2 [line: 86]value:22 [size:0][type:  JSMN_PRIMITIVE]
[line: 78][ 7][type:     JSMN_STRING][size:1]key: 3 [line: 86]value:33 [size:0][type:  JSMN_PRIMITIVE]
[line: 78][ 9][type:     JSMN_STRING][size:1]key: 4 [line: 86]value:44 [size:0][type:  JSMN_PRIMITIVE]
[line: 78][11][type:     JSMN_STRING][size:1]key: 5 [line: 86]value:55 [size:0][type:  JSMN_PRIMITIVE]
[line: 78][13][type:     JSMN_STRING][size:1]key: 6 [line: 86]value:66 [size:0][type:  JSMN_PRIMITIVE]
[line: 78][15][type:     JSMN_STRING][size:1]key: 7 [line: 86]value:77 [size:0][type:  JSMN_PRIMITIVE]
[line: 78][17][type:     JSMN_STRING][size:1]key: 8 [line: 86]value:88 [size:0][type:  JSMN_PRIMITIVE]
[line: 78][19][type:     JSMN_STRING][size:1]key: 9 [line: 86]value:99 [size:0][type:  JSMN_PRIMITIVE]
[line: 78][21][type:     JSMN_STRING][size:1]key: A [line: 86]value:AA [size:0][type:  JSMN_PRIMITIVE]
[line: 78][23][type:     JSMN_STRING][size:1]key: B [line: 86]value:BB [size:0][type:  JSMN_PRIMITIVE]
[line: 78][25][type:     JSMN_STRING][size:1]key: C [line: 86]value:CC [size:0][type:  JSMN_PRIMITIVE]
[line: 78][27][type:     JSMN_STRING][size:1]key: D [line: 86]value:DD [size:0][type:  JSMN_PRIMITIVE]
[line: 78][29][type:     JSMN_STRING][size:1]key: E [line: 86]value:EE [size:0][type:  JSMN_PRIMITIVE]
[line: 78][31][type:     JSMN_STRING][size:1]key: F [line: 86]value:FF [size:0][type:  JSMN_PRIMITIVE]
[line: 78][33][type:     JSMN_STRING][size:1]key: G [line: 86]value:GG [size:0][type:  JSMN_PRIMITIVE]
[line: 78][35][type:     JSMN_STRING][size:1]key: H [line: 86]value:HH [size:0][type:     JSMN_STRING]
[line: 67][37][type:     JSMN_STRING][size:1]key: I [line: 99]value:0 [size:0][type:  JSMN_PRIMITIVE]

.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
                }
            ]
        }

    ]
}

你可能感兴趣的:(物联网)