#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;
}
{“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;
}
運行結果: /* 解析 */
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);