cJSON创建json并万能解析(解析方法通用)
cJSON
Ultralightweight JSON parser in ANSI C
项目地址:https://gitcode.com/gh_mirrors/cj/cJSON

·
前言
连带上次使用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




Ultralightweight JSON parser in ANSI C
最近提交(Master分支:1 个月前 )
424ce4ce
This reverts commit 5b502cdbfb21fbe5f6cf9ffbd2b96e4281a741e6.
Related to #860
10 个月前
32497300 - 11 个月前
更多推荐
所有评论(0)