小白搞定cJSON解析库

一、拿到解析库文件。
这里,我就到交友圣地github下载好这个C语言开源项目。
其作者在Usage开头介绍到cJSON意图成为最 dumb的json解析器,以至于它简单到只有一个源文件和一个头文件,我们可以直接包含到自己工程里面使用。
在这里插入图片描述
二、创建demo程序。
测试环境:Win10 + VS2019

a).两个文件包含到我们测试程序中,并且创建一个工具类CMyJsonParser:在这里插入图片描述
b).解析类里添加Write()和Read()方法:

#pragma once
#include "../cJSON/cJSON.h"

class CMyJsonParser
{
public:
	CMyJsonParser();
	~CMyJsonParser();
public:
	bool WriteJson(const char* pStr);
	bool ReadJson(const char* pStr);
	void PrintJson(cJSON* root);
};

c).实现这三个关键方法:

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

#define MAX_SIZE 1024

CMyJsonParser::CMyJsonParser()
{
}

CMyJsonParser::~CMyJsonParser()
{
}

bool CMyJsonParser::WriteJson(const char* pStr)
{
	bool ret = false;
	const char* filePath = pStr;
	FILE* pFile = fopen(filePath, "w");
	if (pFile)
	{
		cJSON* root = cJSON_CreateObject();
		cJSON* item = cJSON_CreateObject();
		cJSON* next = cJSON_CreateObject();

		cJSON_AddItemToObject(root, "root", cJSON_CreateNumber(0));
		cJSON_AddItemToObject(root, "child1", item);//root节点下添加item节点
		cJSON_AddItemToObject(item, "specificProfile", next);//item节点下添加next节点

		cJSON_AddStringToObject(next, "name", "kunkun");
		cJSON_AddStringToObject(next, "ID", "233333");
		cJSON_AddNumberToObject(next, "age", 16);
		cJSON_AddBoolToObject(next, "male", 1);
		cJSON_AddStringToObject(next, "action", "sing dance rap basketball music.");

		cJSON* item2 = cJSON_CreateObject();
		cJSON* next2 = cJSON_CreateObject();
		cJSON_AddItemToObject(root, "child2", item2);//root节点下添加item节点
		cJSON_AddItemToObject(item2, "specificProfile", next2);//item节点下添加next节点
		cJSON_AddStringToObject(next2, "name", "fanfan");
		cJSON_AddStringToObject(next2, "ID", "322222");
		cJSON_AddNumberToObject(next2, "age", 36);
		cJSON_AddBoolToObject(next2, "male", 1);
		cJSON_AddStringToObject(next2, "action", "dawankuanmian.");

		fprintf(pFile, "%s\n", cJSON_PrintUnformatted(root));
		ret = true;
	}
	return ret;
}
bool CMyJsonParser::ReadJson(const char* pStr)
{
	bool ret = false;
	const char* filePath = pStr;
	FILE* pFile = fopen(filePath, "r");
	if (pFile)
	{
		char buff[MAX_SIZE] = { 0 };
		int nLen = 0;
		while (fgets(buff, MAX_SIZE, pFile) != NULL)
		{
			nLen = strlen(buff);
			buff[nLen - 1] = '\0';  /*去掉换行符*/
			printf("%s\n", buff, nLen - 1);
		}

		cJSON* json = cJSON_Parse(buff);
		if (!json)
		{
			printf("Error:%s\n", cJSON_GetErrorPtr());
			return ret = false;
		}
		PrintJson(json);
		ret = true;
	}

	return ret;
}

void  CMyJsonParser::PrintJson(cJSON* root)
{
	for (int i = 0; i < cJSON_GetArraySize(root); i++)   //遍历最外层json键值对
	{
		cJSON* item = cJSON_GetArrayItem(root, i);
		if (cJSON_Object == item->type)      //如果对应键的值仍为cJSON_Object就递归调用printJson
		{
			PrintJson(item);
		}
		else                                //值不为json对象就直接打印出键和值
		{
			printf("%s->", item->string);
			printf("%s\n", cJSON_Print(item));
		}
	}
}

三、测试看看。

a).Write.

#include "main.h"
#include "JsonParser.h"

int main()
{
	CMyJsonParser parser;
	const char* str = ".\\test.json";
	parser.WriteJson(str);
	cin.get();
	return 0;
}

在这里插入图片描述

这样看不明觉厉,复制到https://www.json.cn/
在这里插入图片描述
b).Read.

int main()
{
	CMyJsonParser parser;
	const char* str = ".\\test.json";
	//parser.WriteJson(str);
	parser.ReadJson(str);
	cin.get();
	return 0;
}

打个断点,看看json对象:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

参考:https://www.jianshu.com/p/4fcb49b55ff6

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值