当前位置: 首页 > 工具软件 > CJsonObject > 使用案例 >

c++ CJsonObject 库解析json

亢嘉茂
2023-12-01
#include "CJsonObject.hpp"


//CJsonObject下载地址    https://github.com/Bwar/CJsonObject

//json解析
		neb::CJsonObject json;

		if (json.Parse(dataStr)) //解析成功返回true
		{
			CELLLog_Info(" CJsonObject Parse Succeed. ");

		/*	int msgid=0;
			if (!json.Get("msgId", msgid))
			{
				CELLLog_Info(" Not Found  msgid ");
			}

			time_t time = 0;
			if (!json.Get("time", time))
			{
				CELLLog_Info(" Not Found  time ");
			}
			std::string data = json("data");*/

			/*cJSON_GetArrayItem(json,);
			cJSON_Parse()*/


			CELLLog_Info(" GetArraySize  %d ", json.GetArraySize());

			//获取数组
			// [1,1591695770646,"hello"]
			int msgId=0;
			if (! json.Get(0, msgId))
			{
				CELLLog_Info(" Not Found  msgid ");
			}
			else
			{
				CELLLog_Info(" msgId  %d ", msgId);
			}

			time_t time = 0;
			if (!json.Get(1, time))
			{
				CELLLog_Info(" Not Found  time ");
			}
			else
			{
				CELLLog_Info(" msgId  %lld ", time);
			}
			std::string data;
			if (!json.Get(2, data))
			{
				CELLLog_Info(" Not Found  data ");
			}
			else
			{
				CELLLog_Info(" data  %s ", data.c_str());
			}
		}
		else
		{
			CELLLog_Info("json.Parse failed.%s",json.GetErrMsg().c_str());
		}
		

 

 类似资料: