用过很多次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);