• 在不知道json格式的情况下如何使用cjson进行解析


    假设我们有一个json字符串,但是我们不知道这个json的组织方式,那么如何进行解析呢,下面就给一个小例子。

    1、我们的json串如下:

    {
        "aStr":    "aaaaaaa",
        "subobject_1":    {
            "bStr":    "bbbbbbb",
            "subobject_2":    {
                "cStr":    "ccccccc"
            }
        },
        "xStr":    "xxxxxxx"
    }

    假设我们只知道这是个json串,不知道这个json都有哪些对象,也不知道这些对象的名字和值,那么现在开始进行解析。

    2、先用cjson把这个字符串解析成cjson能识别的存储方式:

    假设我们这个字符串叫xjson,解析的代码如下:

        char * xjson = 
            "{ 
                "aStr": "aaaaaaa", 
                "subobject_1": 
                { 
                    "bStr": "bbbbbbb", 
                    "subobject_2": 
                    {
                        "cStr": "ccccccc"
                    } 
                }, 
                "xStr": "xxxxxxx" 
            }";
        cJSON * pJson = cJSON_Parse(xjson);

    先判断xjson是什么类型的,再根据不同的类型进行解析:

    #include <string.h>
    #include <stdio.h>
    #include "cJSON.h"
    
    void printntab(int iCnt)
    {
        int i = 0;
        for(i = 0; i < iCnt; i++)
        {
            printf("	");
        }
    }
    
    int parseJson(cJSON * pJson, int iCnt)
    {
        if(NULL == pJson)
        {
            return -1;
        }
        switch(pJson->type)
        {
            case cJSON_False :
                {
                    printf("%s : %d
    ", pJson->string, pJson->valueint);
                }
                break;
            case cJSON_True :
                {
                    printf("%s : %d
    ", pJson->string, pJson->valueint);
                }
                break;
            case cJSON_NULL :
                {
                    printf("%s : NULL
    ", pJson->string);
                }
                break;
            case cJSON_Number :
                {
                    printf("%s : %d | %lf
    ", pJson->string, pJson->valueint, pJson->valuedouble);
                }
                break;
            case cJSON_String :
                {
                    printf("%s : %s
    ", pJson->string, pJson->valuestring);
                }
                break;
            case cJSON_Array  :
            case cJSON_Object :
                {
                    int iSize = cJSON_GetArraySize(pJson);
                    int i = 0;
                    iCnt++;
                    printf("%s : {
    ", NULL == pJson->string ? "" : pJson->string);
                    for(i = 0; i < iSize; i++)
                    {
                        printntab(iCnt);
                        cJSON * pSub = cJSON_GetArrayItem(pJson, i);
                        parseJson(pSub, iCnt);
                    }
                    printntab(iCnt);
                    printf("}
    ");
                }
                break;
            default :
                return -1;
                break;
        }
    }
    
    int main()
    {
        char * xjson =
            "{
                "aStr": "aaaaaaa", 
                "subobject_1": 
                { 
                    "bStr": "bbbbbbb", 
                    "subobject_2": 
                    {
                        "cStr": "ccccccc"
                    } 
                }, 
                "xStr": "xxxxxxx" 
            }";
        cJSON * pJson = cJSON_Parse(xjson);
        if(NULL == pJson)
        {
            return -1;
        }
        parseJson(pJson, 0);
    }

    编译:

    $ g++ -o main main.cpp cjson.c 
    main.cpp: In function ‘int main()’:
    main.cpp:73:3: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

    运行结果:

    $ ./main 
     : {
        aStr : aaaaaaa
        subobject_1 : {
            bStr : bbbbbbb
            subobject_2 : {
                cStr : ccccccc
                }
            }
        xStr : xxxxxxx
        }

    大功告成!

  • 相关阅读:
    Nutch分类搜索
    ubnutu命令行操作以及打开文件
    关于获取字符串的某一段值的问题
    汗。。华夏名网挂了
    “System.OutOfMemoryException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理
    vs2008中无法添加数据连接之解决方案
    DockingManager的使用入门
    C# + .net下使用serialPort做串口开发 1
    为处理 InvalidOperationException. String[1]: the Size property has an invalid size of 0.
    未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll
  • 原文地址:https://www.cnblogs.com/fengbohello/p/4537436.html
Copyright © 2020-2023  润新知