cjson使用方法

1.资源

2.编译

如下几个文件是cjson的核心文件:

cJSON.c
cJSON.h
cJSON_Utils.c
cJSON_Utils.h

其中,可以使用文件:

test.c

作为基础的案例代码,编译与执行方法如下:

$ gcc cJSON.c test.c cJSON_Utils.c
$ ./a.out
Version: 1.7.15
------------test 1----------------------
{
        "name": "Jack (\"Bee\") Nimble",
        "format":       {
                "type": "rect",
                "width":        1920,
                "height":       1080,
                "interlace":    false,
                "frame rate":   24
        }
}
------------test 2----------------------
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
------------test 3----------------------
[[0, -1, 0], [1, 0, 0], [0, 0, 1]]
------------test 4----------------------
{
        "Image":        {
                "Width":        800,
                "Height":       600,
                "Title":        "View from 15th Floor",
                "Thumbnail":    {
                        "Url":  "http:/*www.example.com/image/481989943",
                        "Height":       125,
                        "Width":        "100"
                },
                "IDs":  [116, 943, 234, 38793]
        }
}
------------test 5----------------------
[{
                "precision":    "zip",
                "Latitude":     37.7668,
                "Longitude":    -122.3959,
                "Address":      "",
                "City": "SAN FRANCISCO",
                "State":        "CA",
                "Zip":  "94107",
                "Country":      "US"
        }, {
                "precision":    "zip",
                "Latitude":     37.371991,
                "Longitude":    -122.026,
                "Address":      "",
                "City": "SUNNYVALE",
                "State":        "CA",
                "Zip":  "94085",
                "Country":      "US"
        }]
------------test 6----------------------
{
        "number":       null
}

3.使用

3.1 mqtt应用

3.1.1 设备登录主题(设备上线发送一次)

基于 url: iot/{product_uuid}/{sn}/login,上行:

{
	"sn": "1010",                //设备sn
	"version": "2.1.3"           //嵌入式版本
}

可以使用如下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"

/* Create a bunch of objects as demonstration. */
static int print_preallocated(cJSON *root)
{
    /* declarations */
    char *out = NULL;
    char *buf = NULL;
    size_t len = 0;

    /* formatted print */
    out = cJSON_Print(root);

    /* create buffer to succeed */
    /* the extra 5 bytes are because of inaccuracies when reserving memory */
    len = strlen(out) + 5;
    buf = (char*)malloc(len);
    if (buf == NULL)
    {
        printf("Failed to allocate memory.\n");
        exit(1);
    }

    /* Print to buffer */
    if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
        printf("cJSON_PrintPreallocated failed!\n");
        if (strcmp(out, buf) != 0) {
            printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
            printf("cJSON_Print result:\n%s\n", out);
            printf("cJSON_PrintPreallocated result:\n%s\n", buf);
        }
        free(out);
        free(buf_fail);
        free(buf);
        return -1;
    }

    /* success */
    printf("%s\n", buf);

    free(out);
    free(buf);
    return 0;
}

static void create_objects(void)
{
    cJSON *root = NULL;
    root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "sn", "1010");
    cJSON_AddStringToObject(root,"version","2.1.3");
     /* Print to text */
    if (print_preallocated(root) != 0) {
        cJSON_Delete(root);
        exit(EXIT_FAILURE);
    }
    cJSON_Delete(root);   

}

int main(void)
{
    /* print the version */
    printf("Version: %s\n", cJSON_Version());

    /* Now some samplecode for building objects concisely: */
    create_objects();

    return 0;
}

执行命令:

$ ./a.out
Version: 1.7.15
{
        "sn":   "1010",
        "version":      "2.1.3"
}
3.1.2 设备登录主题(设备下发远程升级命令)

基于 url: iot/{product_uuid}/{sn}/login:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"

/* Create a bunch of objects as demonstration. */
static int print_preallocated(cJSON *root)
{
    /* declarations */
    char *out = NULL;
    char *buf = NULL;
    size_t len = 0;

    /* formatted print */
    out = cJSON_Print(root);

    /* create buffer to succeed */
    /* the extra 5 bytes are because of inaccuracies when reserving memory */
    len = strlen(out) + 5;
    buf = (char*)malloc(len);
    if (buf == NULL)
    {
        printf("Failed to allocate memory.\n");
        exit(1);
    }

    /* Print to buffer */
    if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
        printf("cJSON_PrintPreallocated failed!\n");
        if (strcmp(out, buf) != 0) {
            printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
            printf("cJSON_Print result:\n%s\n", out);
            printf("cJSON_PrintPreallocated result:\n%s\n", buf);
        }
        free(out);
        free(buf);
        return -1;
    }

    /* success */
    printf("%s\n", buf);

    free(out);
    free(buf);
    return 0;
}

static void create_objects(void)
{
    cJSON *root = NULL;
    root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root,"size",13428);
    cJSON_AddStringToObject(root, "url", "https://www.baidu.com");
    cJSON_AddStringToObject(root,"version","3.7.0");
     /* Print to text */
    if (print_preallocated(root) != 0) {
        cJSON_Delete(root);
        exit(EXIT_FAILURE);
    }
    cJSON_Delete(root);   

}

int main(void)
{
    /* print the version */
    printf("Version: %s\n", cJSON_Version());

    /* Now some samplecode for building objects concisely: */
    create_objects();

    return 0;
}

执行结果如下:

在这里插入图片描述

3.1.3 解析数据

可以参看如下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"

/* Create a bunch of objects as demonstration. */
static int print_preallocated(cJSON *root)
{
    /* declarations */
    char *out = NULL;
    char *buf = NULL;
    size_t len = 0;

    /* formatted print */
    out = cJSON_Print(root);

    /* create buffer to succeed */
    /* the extra 5 bytes are because of inaccuracies when reserving memory */
    len = strlen(out) + 5;
    buf = (char*)malloc(len);
    if (buf == NULL)
    {
        printf("Failed to allocate memory.\n");
        exit(1);
    }

    /* Print to buffer */
    if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
        printf("cJSON_PrintPreallocated failed!\n");
        if (strcmp(out, buf) != 0) {
            printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
            printf("cJSON_Print result:\n%s\n", out);
            printf("cJSON_PrintPreallocated result:\n%s\n", buf);
        }
        free(out);
        free(buf);
        return -1;
    }

    /* success */
    printf("%s\n", buf);

    free(out);
    free(buf);
    return 0;
}

static cJSON * prase_json(void)
{
    char *string = "{ \"size\": 10, \"name\": \"zhangsan\"}";
    cJSON *parsed = NULL;
    
    parsed = cJSON_Parse(string);
    return  parsed;
}

int main(void)
{
    static cJSON *cjson_value = NULL; 
    /* print the version */
    printf("Version: %s\n", cJSON_Version());

    /* Now some samplecode for building objects concisely: */
    cjson_value = prase_json();

    if (print_preallocated(cjson_value) != 0) {
        cJSON_Delete(cjson_value);
        return 0;
    }
    cJSON_Delete(cjson_value);   

    return 0;
}

执行结果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值