cJSON创建json并万能解析(解析方法通用)

16 篇文章 1 订阅

前言

连带上次使用cJSON,这是我第二次使用了,由于JSON报文的多样性和不确定性决定了不可能使用查找键去获取对应值得方法(虽然很简单有效),这里我做了些判断,可以解析全未知的json报文(未完善)

代码

#include "cJSON.h"
#include <iostream>
#include <string>
using namespace std;


int main()
{
	/********************************************创建json字串************************************/
	string strJson;
	cJSON * pRoot = NULL;
	pRoot = cJSON_CreateObject();
	if (pRoot)
	{
		cJSON_AddStringToObject(pRoot,"Date","1105");
		cJSON_AddStringToObject(pRoot,"Goal","test");
		cJSON_AddStringToObject(pRoot,"user","ljl");

	}
	char * pcJSONrec = cJSON_Print(pRoot);
	if (pcJSONrec)
	{
		strJson = pcJSONrec;
		free(pcJSONrec);
	}
	cout<< strJson << endl;
	/********************************************创建json数组************************************/
	cJSON * pArr = NULL;
	cJSON * pArrmem = NULL;
	pArr = cJSON_CreateArray();
	if (pArr)
	{
		cJSON_AddItemToArray(pArr,pArrmem=cJSON_CreateObject());
		if (pArrmem)
		{
			cJSON_AddStringToObject(pArrmem,"Belong","BWDA");
			cJSON_AddStringToObject(pArrmem,"Project","AuTOUpdate");

			char * pArrrec = cJSON_Print(pArr);
			string strArr = pArrrec;
			cout << strArr << endl;

			if (pArrrec)
			{
				free(pArrrec);
			}
			/*将json数组插入json字串*/
#if 0
			cJSON_AddStringToObject(pRoot,"InFo",pArrrec);
			pcJSONrec = cJSON_Print(pRoot);
			if (pcJSONrec)
			{
				strJson = pcJSONrec;
				free(pcJSONrec);
			}
			cout << strJson << endl;

#endif
			cJSON_AddItemToObject(pRoot,"InFo",pArr);
			pcJSONrec = cJSON_Print(pRoot);
			if (pcJSONrec)
			{
				strJson = pcJSONrec;
				free(pcJSONrec);
			}
			cout << strJson << endl;
		}
	}

	if (pRoot)
	{
		cJSON_Delete(pRoot);
		pRoot = NULL;
		cout << "pRoot被释放" << endl;
	}
/************************************************解析json**************************************/
#if 1
	cJSON *AnalysisJson = NULL;
	AnalysisJson = cJSON_Parse(strJson.c_str());
	if (AnalysisJson)
	{
		/******************************解析未知格式json字串***********************/
		cout << "解析未知格式json字串" << endl; 
		cJSON *pCjson = AnalysisJson->child;
		while (pCjson)
		{
			switch(pCjson->type)
			{
			    case cJSON_False:
					cout << pCjson->string  << " " << pCjson->valueint << endl;
					break;
				case cJSON_True:
					cout << pCjson->string << " " << pCjson->valueint << endl;
					break;
				case cJSON_NULL:
					cout << pCjson->string << endl;
					break;
				case cJSON_Number:
					cout << pCjson->string << " " << pCjson->valueint << " " << pCjson->valuedouble << endl;
					break;
				case cJSON_String:
					cout << pCjson->string << " " << pCjson->valuestring << endl;
					break;
				case cJSON_Array:
					{
						/**************解析json数组******************/
						int isize = cJSON_GetArraySize(pCjson);
						cout << "数组键为" << pCjson->string << endl;
						cout << "数组大小为" << isize << endl;
						//char * pArr = cJSON_Print(pCjson);
						//cout << pArr << endl;
						//json数组pCjson的子节点是json对象
						//获取json对象pChild
						cJSON * pArrChild = pCjson->child;
						if (pArrChild)
						{
							cJSON * ppACChild = pArrChild->child;
							while (ppACChild)
							{
								switch(ppACChild->type)
								{
								case cJSON_False:
									cout << ppACChild->string  << " " << ppACChild->valueint << endl;
									break;
								case cJSON_True:
									cout << ppACChild->string << " " << ppACChild->valueint << endl;
									break;
								case cJSON_NULL:
									cout << ppACChild->string << endl;
									break;
								case cJSON_Number:
									cout << ppACChild->string << " " << ppACChild->valueint << " " << ppACChild->valuedouble << endl;
									break;
								case cJSON_String:
									cout << ppACChild->string << " " << ppACChild->valuestring << endl;
									break;
								}
								ppACChild = ppACChild->next;
							}
						}
					}
					break;
				case cJSON_Object:
					cout<< "cJSON_Object暂时未解析" << endl;
					break;
				default:
					break;
			}
			pCjson = pCjson->next;
		}
	}

#endif


	return 0;
}


 尾言

由于创建JSON字串时是知晓键的命名,因此可以直接组装JSON报文,但如果想做成通用的,这里提供一个思路:将JSON键值用vector<pair<string,string>>存储,然后依次读取vector组装JSON,但该方法只能用于结构简单单一的JSON报文,如果结构复杂例如存在JSON数组,这个方法就不行了

我的解析JSON方法还有一个未完善,等下次用到时再完善

cJSON中对type的定义

/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6

 

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值