cJson庫的使用

  用過很多次cJson庫,都是比較簡單的使用,而且每次用了都會忘記,只能去翻曾經寫過的代碼或是重新到網上查找下,今天在這裏記錄下曾經有用到過的json數據的拼裝和解析。
  至於什麼是JSON和C語言庫在哪查找,不懂的可以自行百度。
 json數據拼裝
簡單結構
  我們先來拼裝一個簡單的Json數據:存儲學生張三的信息,他的信息有number(學號),name(姓名),class(班級),score(成績),我們先對其進行拼裝,然後打印,實現的代碼如下:
#include "stdafx.h"
#include "cJson.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
	cJSON * pJsonRoot;
	pJsonRoot = cJSON_CreateObject();
	if (pJsonRoot == NULL){
		return -1;
	}
	cJSON_AddStringToObject(pJsonRoot, "number", "0001");/* 增加字符串數據 */
	cJSON_AddStringToObject(pJsonRoot, "name", "張三");
	cJSON_AddStringToObject(pJsonRoot, "class", "140412");
	cJSON_AddNumberToObject(pJsonRoot, "score", 90);/* 增加整型數據 */
	char *szJson = cJSON_Print(pJsonRoot);/* 將json結構轉換爲字符串 */
	if (szJson != NULL){
		printf("%s\n", szJson);
		free(szJson);/* 需要釋放 */
	}

	cJSON_Delete(pJsonRoot);/* 釋放資源 */
	return 0;
}
   運行結果如圖所示:
  
數組與嵌套
  有時候我們需要用一個Json數據結構來顯示一個班級信息,需要保存班級的每個學生信息,類似於:
{“class”: “140412”
[
{“number”:“14041201”, “name”:“張三”},
{“number”:“14041202”, “name”:“李四”},
{“number”:“14041203”, “name”:“王五”}
....
]
}
   這就需要用到json數組了, 我們先來實現一個json數組,存儲0-9這幾個數字,然後打印,代碼如下:
#include "stdafx.h"
#include "cJson.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
	cJSON * pJsonRoot;
	pJsonRoot = cJSON_CreateArray();
	if (pJsonRoot == NULL){
		return -1;
	}
	for (int i = 0; i < 10; i++){
		cJSON_AddNumberToObject(pJsonRoot, "", i);
	}
	char *szJson = cJSON_Print(pJsonRoot);/* 將json結構轉換爲字符串 */
	if (szJson != NULL){
		printf("%s\n", szJson);
		free(szJson);/* 需要釋放 */
	}

	cJSON_Delete(pJsonRoot);/* 釋放資源 */
	return 0;
}
運行結果如下圖:

  上述的結果顯然是不能滿足我們保存一個班級的所有學生信息,因爲學生有諸如學號、姓名等多個屬性,如果我們用json來實現的話數組裏面裝的應該是cJSON對象,這就涉及到對象的嵌套了,其實JSON對象的嵌套是很簡單的,直接用cJSON_AddItemToObject()函數將一個對象加進來即可,實現保存學生信息的數組簡要代碼如下:
#include "stdafx.h"
#include "cJson.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
	cJSON * pJsonRoot;
	pJsonRoot = cJSON_CreateArray();
	if (pJsonRoot == NULL){
		return -1;
	}
	char *szNumArray[] = { "0001", "0002", "0003", "0004" };
	char *szNameArray[] = { "張三", "李四", "王五", "趙六" };
	for (int i = 0; i < _countof(szNameArray); i++){
		cJSON *pItem = cJSON_CreateObject();/* 創建一個子Json對象 */
		cJSON_AddStringToObject(pItem, "number", szNumArray[i]);
		cJSON_AddStringToObject(pItem, "name", szNameArray[i]);
		cJSON_AddItemToObject(pJsonRoot, "", pItem);
	}
	char *szJson = cJSON_Print(pJsonRoot);/* 將json結構轉換爲字符串 */
	if (szJson != NULL){
		printf("%s\n", szJson);
		free(szJson);/* 需要釋放 */
	}

	cJSON_Delete(pJsonRoot);/* 釋放資源 */
	return 0;
}
運行結果:

   這時候我們實現上面提到的班級管理就比較簡單了,就是在上層再增加一個嵌套,代碼如下:
#include "stdafx.h"
#include "cJson.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
	cJSON *pClassRoot;
	cJSON * pJsonRoot;
	pClassRoot = cJSON_CreateObject();
	if (pClassRoot == NULL){
		return -1;
	}
	cJSON_AddStringToObject(pClassRoot, "class", "10000");
	pJsonRoot = cJSON_CreateArray();
	if (pJsonRoot == NULL){
		cJSON_Delete(pClassRoot);/* 釋放資源 */
		return -1;
	}

	char *szNumArray[] = { "0001", "0002", "0003", "0004" };
	char *szNameArray[] = { "張三", "李四", "王五", "趙六" };
	for (int i = 0; i < _countof(szNameArray); i++){
		cJSON *pItem = cJSON_CreateObject();/* 創建一個子Json對象 */
		cJSON_AddStringToObject(pItem, "number", szNumArray[i]);
		cJSON_AddStringToObject(pItem, "name", szNameArray[i]);
		cJSON_AddItemToObject(pJsonRoot, "", pItem);
	}
	cJSON_AddItemToObject(pClassRoot, "student", pJsonRoot);
	char *szJson = cJSON_Print(pClassRoot);/* 將json結構轉換爲字符串 */
	if (szJson != NULL){
		printf("%s\n", szJson);
		free(szJson);/* 需要釋放 */
	}

	cJSON_Delete(pClassRoot);/* 釋放資源 */
	return 0;
}
 運行結果:


 json數據解析
    有了上面拼裝好的數據,我們直接寫代碼進行解析,這裏我只實現簡要的代碼,重在邏輯,代碼如下:
	/* 解析 */
	cJSON *pParseRoot = cJSON_Parse(szJson);/* 將字符串格式的JSON數據轉化爲JSON數據結構 */
	if (pParseRoot == NULL){
		return -1;
	}
	cJSON *pClass = cJSON_GetObjectItem(pParseRoot, "class");/* 獲取班級的對象 */
	printf("class=%s\n", pClass->valuestring);/* 打印班級信息,如果是整型就用pClass->valueint */
	cJSON *pArray = cJSON_GetObjectItem(pParseRoot, "student");
	int nCount = cJSON_GetArraySize(pArray);/* 獲取JSON數組大小 */
	for (int i = 0; i < nCount; i++){
		cJSON *pStudent = cJSON_GetArrayItem(pArray, i);
		cJSON * pNum = cJSON_GetObjectItem(pStudent, "number");
		cJSON * pName = cJSON_GetObjectItem(pStudent, "name");
		printf("number=%s name=%s\n",pNum->valuestring, pName->valuestring);
	}
	cJSON_Delete(pParseRoot);



發佈了34 篇原創文章 · 獲贊 25 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章