Json   发布时间:2019-10-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jsoncpp使用方法总结大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

// jsoncpp使用方法总结@H_801_1@ // 结构体数据转换为json字符串@H_801_1@ // json字符串转换为结构体数据

// jsoncpp下载地http://sourceforge.net/projects/jsoncpp/

// 样例代码如下

#include <stdio.h>
#include <String.h>
#include <memory.h>
#include <exception>
#include <String>
#include "json/json.h"

// 测试使用的结构体
typedef struct {
	int nNum;
	char szFile[20];
}test_ST_FILE;

typedef struct {
	char szMemo[20];
	int nCount;
	test_ST_FILE szFileList[5];
}test_ST_FILE_LIST;

// 结构体数据转换为JSON字符串
int StructDataToJsonString(test_ST_FILE_LIST *pStructData,char *pJsonData);
// JSON字符串转换为结构体数据
int JsonStringToStructData(char *pJsonData,test_ST_FILE_LIST *pStructData);

// 主函数
int main(int argc,char *argv[])
{
	test_ST_FILE_LIST stFileList;
	char szJsonData[4096];

	memset(&stFileList,sizeof(test_ST_FILE_LIST));
	memset(szJsonData,sizeof(szJsonData));

	// 测试数据
	strncpy(stFileList.szMemo,"jsoncppmemo",sizeof(stFileList.szMemo));
	stFileList.nCount = 5;
	for(int i = 0; i < stFileList.nCount; i++)
	{
		stFileList.szFileList[i].nNum = i + 1;
		sprintf(stFileList.szFileList[i].szFile,"file%d",i + 1);
	}
	printf("struct data to json String.\n");
	printf("memo:%s count:%d.\n",stFileList.szMemo,stFileList.nCount);
	for(int i = 0; i < stFileList.nCount; i++)
	{
		printf("num:%d file:%s.\n",stFileList.szFileList[i].nNum,stFileList.szFileList[i].szFilE);
	}

	// 结构体数据转换为JSON字符串
	StructDataToJsonString(&stFileList,szJsonData);
	printf("json String:%s.\n",szJsonData);

	// JSON字符串转换为结构体数据
	memset(&stFileList,sizeof(test_ST_FILE_LIST));
	JsonStringToStructData(szJsonData,&stFileList);
	printf("json String to struct data.\n");
	printf("memo:%s count:%d.\n",stFileList.szFileList[i].szFilE);
	}
	return 0;
}

// 结构体数据转换为JSON字符串
int StructDataToJsonString(test_ST_FILE_LIST *pStructData,char *pJsonData)
{
	try
	{
		Json::Value root;
		Json::Value arrayObj;
		Json::Value item;

		root["memo"] = pStructData->szMemo;
		root["file_count"] = pStructData->nCount;
		// 生成file_list数组
		for(int i = 0; i < pStructData->nCount; i ++)
		{
			item["num"] = pStructData->szFileList[i].nNum;
			item["file"] = pStructData->szFileList[i].szFile;
			arrayObj.append(item);
		}
		root["file_list"] = arrayObj;
		// JSON转换为JSON字符串(已格式化)
		std::string strOut = root.toStyledString();
		//  JSON转换为JSON字符串(未格式化)
		//Json::FastWriter writer;
		//std::string strOut = writer.write(root);

		strcpy(pJsonData,strOut.c_str());
	}
	catch(std::exception &eX)
	{
		printf("StructDataToJsonString exception %s.\n",ex.what());
		return -1;
	}
	return 0;
}

// JSON字符串转换为结构体数据
int JsonStringToStructData(char *pJsonData,test_ST_FILE_LIST *pStructData)
{
	try
	{
		bool bRet = false;
		std::string strTemp;
		Json::reader reader;
		Json::Value value;
		Json::Value arrayObj;
		Json::Value item;

		// JSON字符串转换为JSON数据
		bRet = reader.parse(pJsonData,value);
		if(bRet == falsE)
		{
			printf("JsonStringToStructData reader parse error.\n");
			return -1;
		}
		// memo
		bRet = value["memo"].empty();
		if(bRet == truE)
		{
			printf("JsonStringToStructData memo is not exist.\n");
			return -1;
		}
		strTemp = value["memo"].asString();
		strncpy(pStructData->szMemo,strTemp.c_str(),sizeof(pStructData->szMemo));
		// file_count
		bRet = value["file_count"].empty();
		if(bRet == truE)
		{
			printf("JsonStringToStructData file_count is not exist.\n");
			return -1;
		}
		pStructData->nCount = value["file_count"].asInt();

		// 解析file_list数组
		arrayObj = value["file_list"];
		pStructData->nCount = arrayObj.size();
		for(int nIdx = 0; nIdx < pStructData->nCount; nIdx++)
		{
			item = arrayObj[nIdx];
			// num
			pStructData->szFileList[nIdx].nNum = item["num"].asInt();
			// file
			strTemp = item["file"].asString();
			strncpy(pStructData->szFileList[nIdx].szFile,sizeof(pStructData->szFileList[nIdx].szFilE));
		}
	}
	catch(std::exception &eX)
	{
		printf( "JsonStringToStructData exception %s.\n",ex.what());
		return -1;
	}
	return 0;
}

@H_801_1@

执行结果@H_801_1@ struct data to json String.@H_801_1@ memo:jsoncppmemo count:5.@H_801_1@ num:1 file:file1.@H_801_1@ num:2 file:file2.@H_801_1@ num:3 file:file3.@H_801_1@ num:4 file:file4.@H_801_1@ num:5 file:file5.

json String:{@H_801_1@ "file_count" : 5,@H_801_1@ "file_list" : [@H_801_1@ {@H_801_1@ "file" : "file1",@H_801_1@ "num" : 1@H_801_1@ },@H_801_1@ {@H_801_1@ "file" : "file2",@H_801_1@ "num" : 2@H_801_1@ },@H_801_1@ {@H_801_1@ "file" : "file3",@H_801_1@ "num" : 3@H_801_1@ },@H_801_1@ {@H_801_1@ "file" : "file4",@H_801_1@ "num" : 4@H_801_1@ },@H_801_1@ {@H_801_1@ "file" : "file5",@H_801_1@ "num" : 5@H_801_1@ }@H_801_1@ ],@H_801_1@ "memo" : "jsoncppmemo"@H_801_1@ }@H_801_1@ .

json String to struct data. memo:jsoncppmemo count:5. num:1 file:file1. num:2 file:file2. num:3 file:file3. num:4 file:file4. num:5 file:file5.

大佬总结

以上是大佬教程为你收集整理的jsoncpp使用方法总结全部内容,希望文章能够帮你解决jsoncpp使用方法总结所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。