文件路径json格式存储

项目需要以json格式存储指定文件目录结构,如需求如下所示:

文件路径json格式存储_第1张图片

简要思路,遍历指定目录结构,存储在list中,然后,取出每一条路径结构,进行json格式化,

与已有json格式路径对象相加。

生成json格式代码:

BOOL _makeValue(Json::Value & ret, const PFILEINFO pfileInfor)
{
	BOOL blRet = FALSE;
	if (pfileInfor == nullptr)
	{
		_tprintf(_T("invlaid input value."));
		return FALSE;
	}
	Json::Reader read;
	Json::Value root;
	Json::Value val;
	TStringList lstFileFolder;
	Split(lstFileFolder, pfileInfor->strFullPath.c_str(), _T("\\"), TRUE);

	for (auto rit = lstFileFolder.rbegin(); rit != lstFileFolder.rend(); rit++)
	{
		TString strfile = *rit;
		if (rit == lstFileFolder.rbegin())
		{
			TString strExt = PathFindExtension(pfileInfor->strFullPath.c_str());
			if (!strExt.empty())
			{
				root["length:"] = pfileInfor->ullLength;
				root["size:"] = (double)pfileInfor->ullSize;
				val[converttostring(*rit)] = root;
			}
			else
			{
				root.clear();
				val[converttostring(*rit)] = root;
			}
			ret = val;
			blRet = TRUE;
		}
		else
		{
			val.clear();
			val[converttostring(*rit)] = ret;
			ret.clear();
			ret = val;
		}
	}
	_tprintf(_T("make value:%s"), ret.toStyledString().c_str());
	return blRet;
}

json格式对象,累加处理代码:

Json::Value _add(const Json::Value & other, const Json::Value & another)
{
	Json::Value ret = another;
	BOOL blFound = FALSE;
	auto lstother = other.getMemberNames();
	auto lstanoter = another.getMemberNames();
	for each (std::string varOther in lstother)
	{
		for each (std::string varAnoter in lstanoter)
		{
			if (strcmp(varOther.c_str(), varAnoter.c_str()) == 0)
			{
				_tprintf(_T("%s exist in two"), varOther.c_str());
				blFound = TRUE;
			}
		}
		if (blFound)
		{
			ret[varOther] = _add(other[varOther], another[varOther]);
		}
		else
		{
			ret[varOther] = other[varOther];
		}
	}
	return ret;
}

ps:代码中,需要考虑异常情况。

 

 

你可能感兴趣的:(C/C++,Json,c,Json,文件路)