15_Linux应用编程cJSON库的使用_cjson库下载

Linux应用编程cJSON库的使用

cJSON库的下载

git链接:https://github.com/moonright/cJSON

cJSON库的引用

该库非常简单,下载解压后如下图,正常使用时只需要cJSON.h和cJSON.c两个文件,将两个文件放入到项目中,引用cJSON.h头文件即可。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

示例

#include <iostream>
#include <string.h>
#include <stdio.h>
#include<time.h>
#include "cJSON-master/cJSON.h"

using namespace std;

//创建json字符串
void creat_cjsonstr(char *cjsonstr, int max_len)
{
    char *tmp_str;
    cJSON *root = NULL;
    cJSON *root1 = NULL;
    cJSON *array = NULL;
    cJSON *mother = NULL;
    cJSON *father = NULL;


    //创建一个cJSON对象
    root = cJSON_CreateObject();
    //添加一个字符串
    cJSON_AddStringToObject(root, "name", "Dave");
    //添加一个数字
    cJSON_AddNumberToObject(root, "age", 24);

    //创建嵌套cJSON对象
    root1 = cJSON_CreateObject();
    cJSON_AddItemToObject(root, "other", root1);
    //root1 = cJSON_AddObjectToObject(root, "other");
    //创建cJSON数组
    array = cJSON_CreateArray();
    cJSON_AddItemToObject(root1, "relative", array);

    //向数组中添加无名对象
    father = cJSON_CreateObject();
    cJSON_AddItemToArray(array, father);
    cJSON_AddStringToObject(father, "name", "Jonh");
    cJSON_AddNumberToObject(father, "age", 46);

    mother = cJSON_CreateObject();
    cJSON_AddItemToArray(array, mother);
    cJSON_AddStringToObject(mother, "name", "Lisa");
    cJSON_AddNumberToObject(mother, "age", 46);



    //添加一个空的对象,并返回节点的名称
    cJSON *object = cJSON_AddObjectToObject(root1, "object");
    cJSON *arrays = cJSON_AddArrayToObject(root1, "array");

    // 打印 JSON 字符串
    tmp_str = cJSON_Print(root);
    //printf("%s\n", tmp_str);

    //拷贝数据
    memcpy(cjsonstr, tmp_str, strlen(tmp_str));

    // 清理内存
    cJSON_free(tmp_str);
    cJSON_Delete(root);
}

//解码json字符串
void parse_cjsonstr(char *str)
{
    cJSON *root = NULL;
    cJSON *other = NULL;
    cJSON *relative = NULL;
    cJSON *element = NULL;

    root = cJSON_Parse(str);
    other = cJSON_GetObjectItemCaseSensitive(root, "other");
    relative = cJSON_GetObjectItemCaseSensitive(other, "relative");

    printf("root_name: %s\n", cJSON_GetStringValue(cJSON_GetObjectItemCaseSensitive(root, "name")));

    cJSON_ArrayForEach(element, relative)
    {
        printf("name: %s\n", cJSON_GetStringValue(cJSON_GetObjectItemCaseSensitive(element, "name")));
        printf("age: %d\n", (int)cJSON_GetNumberValue(cJSON_GetObjectItemCaseSensitive(element, "age")));
    }

    cJSON_Delete(root);
}

int main(int argc, char *argv[])
{
    char str[800] = {0};
    creat_cjsonstr(str, 800);
    printf("%s\n", str);
    parse_cjsonstr(str);

        return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

在这里插入图片描述