文章目录

在前面简单介绍了cJSON相关的函数,下面建创建以及解析json数据。

1 创建

假如我们要创建如下的json数据

{
    "GET_WIFI":
    {
        "WIFI_COUNT" : 3,
        "WIFISSID":[
        {
            "SSID": "test_wifi01",
            "SIGNAL": 99,
            "SECURITY": 0
        },
        {
            "SSID": "test_wifi02"
            "SIGNAL": 88,
            "SECURITY": 1
        },
        {
            "SSID": "test_wifi03"
            "SIGNAL": 77
            "SECURITY": 2
        }
        ]
    }
}

代码如下:

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

typedef struct _SSID_INFO
{
    char ssid[32];
    int  signal;
    int security;
}SSID_INFO_S;

int test_cjon_build()
{
    int ret = -1;
    SSID_INFO_S Info[3] = {{"test_wifi01",99,0},{"test_wifi02",88,1},{"test_wifi03",77,2}};
    cJSON* pHead = NULL;
    cJSON* pGetWifi = NULL;
    cJSON* pWifiCnt = NULL;
    cJSON* pWifiSsidArray = NULL;
    cJSON* pWifiSsid = NULL;
    cJSON* pSsid = NULL;
    cJSON* pSignal = NULL;
    cJSON* pSecurity = NULL;
    char* tmpBuff = NULL;
    int i = 0, cnt = 3;

    /* 创建json对象 */
    pHead = cJSON_CreateObject();
    if (NULL == pHead) goto end;

    /* 创建GET_WIFI对象并添加到头中 */
    pGetWifi = cJSON_CreateObject();
    if (NULL == pGetWifi) goto end;

    cJSON_AddItemToObject(pHead, "GET_WIFI", pGetWifi);
    
    /* 创建WIFI_COUNT并添加到GET_WIFI中 */
    pWifiCnt = cJSON_CreateNumber(cnt);
    if (NULL == pWifiCnt) goto end;

    cJSON_AddItemToObject(pGetWifi, "WIFI_COUNT", pWifiCnt);
    
    /* 创建WIFISSID数组 */
    pWifiSsidArray = cJSON_CreateArray(); 
    if (NULL == pWifiSsidArray) goto end;
    
    cJSON_AddItemToObject(pGetWifi, "WIFISSID", pWifiSsidArray);

    for (i = 0; i < cnt; i++)
    {
        /* 创建pWifiSsid对象并添加到WIFISSID数组中 */
        pWifiSsid = cJSON_CreateObject();
        if (NULL == pWifiSsid) goto end;
        cJSON_AddItemToArray(pWifiSsidArray,pWifiSsid);
        
        /* 将各个值添加到pWifiSsid对象中 */
        pSsid = cJSON_CreateString(Info[i].ssid);
        if (NULL == pSsid) goto end;
        cJSON_AddItemToObject(pWifiSsid, "SSID", pSsid);
        
        pSignal = cJSON_CreateNumber(Info[i].signal);
        if (NULL == pSignal) goto end;
        cJSON_AddItemToObject(pWifiSsid, "SIGNAL", pSignal);
        
        pSecurity = cJSON_CreateNumber(Info[i].security);
        if (NULL == pSecurity) goto end;
        cJSON_AddItemToObject(pWifiSsid, "SECURITY", pSecurity);
    }
    
    /* 输出为字符串形式 */
    tmpBuff = cJSON_Print(pHead);
    if (NULL == tmpBuff) goto end;
   
    printf("tmpBuff:%s\n",tmpBuff);
    /*
    tmpBuff:{
	"GET_WIFI":	{
		"WIFI_COUNT":	3,
		"WIFISSID":	[{
				"SSID":	"test_wifi01",
				"SIGNAL":	99,
				"SECURITY":	0
			}, {
				"SSID":	"test_wifi02",
				"SIGNAL":	88,
				"SECURITY":	1
			}, {
				"SSID":	"test_wifi03",
				"SIGNAL":	77,
				"SECURITY":	2
			}]
	}
}
    */
    ret = 0;
    end:
    /* 释放内存 */
    cJSON_Delete(pHead);
    if (NULL != tmpBuff) 
        free(tmpBuff);
    return ret;
}

2 解析

对上面的数据进行解析,代码如下:

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

#define TEST_STRING "{" \
    "\"GET_WIFI\":" \
    "{" \
        "\"WIFI_COUNT\" : 3," \
        "\"WIFISSID\":[" \
        "{" \
            "\"SSID\": \"test_wifi01\"," \
            "\"SIGNAL\": 99," \
            "\"SECURITY\": 0" \
        "}," \
        "{" \
            "\"SSID\": \"test_wifi02\"," \
            "\"SIGNAL\": 88," \
            "\"SECURITY\": 1" \
        "}," \
        "{" \
            "\"SSID\": \"test_wifi03\"," \
            "\"SIGNAL\": 77," \
            "\"SECURITY\": 2" \
        "}" \
        "]" \
    "}" \
"}"

int test_cjon_parse()
{
    int ret = -1;
    char* error = NULL;
    int cnt = 0, i = 0;
    cJSON* pHead = NULL;
    cJSON* pGetWifi = NULL;
    cJSON* pWifiCnt = NULL;
    cJSON* pWifiSsidArray = NULL;
    cJSON* pWifiSsid = NULL;
    cJSON* pSsid = NULL;
    cJSON* pSignal = NULL;
    cJSON* pSecurity = NULL;
    
    pHead = cJSON_Parse(TEST_STRING);
    if (NULL == pHead)
    {
        error = cJSON_GetErrorPtr();
        if (NULL != error)
            printf("error:%s\n", error);
        goto end;
    }
    
    pGetWifi = cJSON_GetObjectItem(pHead, "GET_WIFI");
    if (NULL == pGetWifi) goto end;
    
    pWifiCnt = cJSON_GetObjectItem(pGetWifi, "WIFI_COUNT");
    if (NULL == pWifiCnt) goto end;
    
    cnt = pWifiCnt->valuedouble;
    
    pWifiSsidArray = cJSON_GetObjectItem(pGetWifi, "WIFISSID");
    if (NULL == pWifiSsidArray) goto end;
    
    cJSON_ArrayForEach(pWifiSsid, pWifiSsidArray)
    {
        pSsid = cJSON_GetObjectItem(pWifiSsid, "SSID");
        pSignal = cJSON_GetObjectItem(pWifiSsid, "SIGNAL");
        pSecurity = cJSON_GetObjectItem(pWifiSsid, "SECURITY");
        
        printf("total wifi num:%d, current num:%d, wifi ssid:%s, wifi signal:%d, wifi security:%d\n",
                cnt, ++i, pSsid->valuestring, (int)pSignal->valuedouble, (int)pSecurity->valuedouble);
    }
    
    ret = 0;
    end:
    /* 释放内存 */
    cJSON_Delete(pHead);
    return ret;
}

输出结果如下:

total wifi num:3, current num:1, wifi ssid:test_wifi01, wifi signal:99, wifi security:0
total wifi num:3, current num:2, wifi ssid:test_wifi02, wifi signal:88, wifi security:1
total wifi num:3, current num:3, wifi ssid:test_wifi03, wifi signal:77, wifi security:2

GitHub 加速计划 / cj / cJSON
10.45 K
3.16 K
下载
Ultralightweight JSON parser in ANSI C
最近提交(Master分支:2 个月前 )
424ce4ce This reverts commit 5b502cdbfb21fbe5f6cf9ffbd2b96e4281a741e6. Related to #860 4 个月前
32497300 - 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐