C++ 判断一个 字符串是不是 json 字符串

来源:互联网 发布:dnf数据芯片要刷全图吗 编辑:程序博客网 时间:2024/05/20 10:54

这个接口只是用来判断一个字符串是不是json 结构的字符串,不具备json字符串的 parse 功能。


例如判断下面的字符串是个 正确的 json

json1 示例:

{
    "aaa": "1000",
    "bbb": "2000"
}


json2 示例:

{
    "aaa": "1000",
    "bbb": "2000",
    "array": [
        {
            "c": "30000",
            "d": "40000"
        },
        {
            "c": "30000",
            "d": "40000"
        }
    ]
}



下面的是C++ 实现判断的代码:


#ifndef JSON_WSTR_JUDGE_HELPER_HPP
#define JSON_WSTR_JUDGE_HELPER_HPP
#include <string>
#include <vector>
namespace json_wstr_judge_helper{




std::wstring RemoveWcharFromWstr(std::wstring wstr, WCHAR wch)
{
for (std::wstring::iterator iter = wstr.begin(); iter != wstr.end(); iter++)
{
if (wch == *iter)
{
wstr.erase(iter);
iter--;
}
}


return wstr;
}


BOOL IsSimpleJsonObjectString(std::wstring wstrJson)
{
wstrJson = RemoveWcharFromWstr(wstrJson, L' ');
wstrJson = RemoveWcharFromWstr(wstrJson, L'\n');
wstrJson = RemoveWcharFromWstr(wstrJson, L'\t');


// 格式化后的字符串一定是这个样子,需要判断冒号和逗号
//{"*":"*","*":"*","*":"*"} 


if (L'{' != wstrJson[0])
{
return FALSE;
}


if (L'}' != wstrJson[wstrJson.length() - 1])
{
return FALSE;
}




UINT uLen = wstrJson.length();
for (UINT i = 4; i < uLen; i += 8)
{
if (L':' != wstrJson[i])
{
return FALSE;
}


UINT j = i + 4;
if ((j < uLen - 1) && (L',' != wstrJson[j]))
{
return FALSE;
}
}




return TRUE;
}




BOOL IsSimpleStringArray(std::wstring wstrJson)
{
wstrJson = RemoveWcharFromWstr(wstrJson, L' ');
wstrJson = RemoveWcharFromWstr(wstrJson, L'\n');
wstrJson = RemoveWcharFromWstr(wstrJson, L'\t');


// 这会儿已经被格式化成 ["*","*","*"]
UINT uLen = wstrJson.length();
if (L'[' != wstrJson[0])
{
return FALSE;
}


if (L']' != wstrJson[uLen - 1])
{
return FALSE;
}


for (UINT i = 4; i < uLen - 2; i += 4)
{
if (L',' != wstrJson[i])
{
return FALSE;
}
}


return TRUE;
}


std::wstring simplifyWstring(std::wstring wstrJson)
{


wstrJson = RemoveWcharFromWstr(wstrJson, L' ');
wstrJson = RemoveWcharFromWstr(wstrJson, L'\n');
wstrJson = RemoveWcharFromWstr(wstrJson, L'\t');


std::vector<WCHAR> vecQuotesPos;
std::wstring wstrTemp = wstrJson;


// 找到字符串中的所有双引号
UINT uLen = wstrJson.length();
for (UINT u = 0; u < uLen; u++)
{
if (L'\"' == wstrJson[u])
{
vecQuotesPos.push_back(u);
}
}


// 把双引号之间的字符串替换成*字符串
int uvecQuotesPosSize = vecQuotesPos.size();
for (int uNum = uvecQuotesPosSize - 1; uNum > 0; uNum -= 2)
{
wstrTemp = wstrTemp.substr(0, vecQuotesPos[uNum - 1] + 1) + L"*" + wstrTemp.substr(vecQuotesPos[uNum], -1);
}


return wstrTemp;
}


BOOL BChangeJson(std::wstring  wstrJson)
{
int uPosRightBigBraces = 0; // 大括号
int uPosRightMidBrackets = 0;//中括号


uPosRightBigBraces = wstrJson.find(L'}');
uPosRightMidBrackets = wstrJson.find(L']');




if ((0 <= uPosRightBigBraces && uPosRightBigBraces < uPosRightMidBrackets) || (0 <= uPosRightBigBraces && uPosRightMidBrackets < 0))
{
// 最先要处理的是一个大括号
int nPosLeft = wstrJson.rfind(L'{', uPosRightBigBraces);


if (nPosLeft < 0)
{
return FALSE;
}


std::wstring wstrSubJson = wstrJson.substr(nPosLeft, uPosRightBigBraces - nPosLeft + 1);
if (IsSimpleJsonObjectString(wstrSubJson))
{
wstrJson = wstrJson.substr(0, nPosLeft) + L"\"*\"" + wstrJson.substr(uPosRightBigBraces + 1, -1);
}
else
{
return FALSE;
}
}
else if ((uPosRightBigBraces > uPosRightMidBrackets && uPosRightMidBrackets >= 0) || (uPosRightMidBrackets >= 0 && uPosRightBigBraces < 0))
{
// 最先要处理的是一个中括号
int nPosLeft = wstrJson.rfind(L'[', uPosRightMidBrackets);


if (nPosLeft < 0)
{
return FALSE;
}


std::wstring wstrSubJson = wstrJson.substr(nPosLeft, uPosRightMidBrackets - nPosLeft + 1);
if (IsSimpleStringArray(wstrSubJson))
{
wstrJson = wstrJson.substr(0, nPosLeft) + L"\"*\"" + wstrJson.substr(uPosRightMidBrackets + 1, -1);
}
else
{
if (wstrJson == L"\"*\"")
{
return TRUE;
}
else
{
return  FALSE;
}
}
}
else if (L"\"*\"" == wstrJson)
{
// 刚开始如果是个 L"*" ,已经 返回 false 了
return TRUE;
}
else
{
// 非法字符串 如: "sffsdfgds"
return FALSE;
}


return BChangeJson(wstrJson);
}



BOOL IsJsonString(std::wstring wstrJson)
{
std::wstring wstrTemp = simplifyWstring(wstrJson);


// 把json中的Object 转化成 "*", 把数组转化成 "*"
if (wstrTemp == L"\"*\"")
{
return FALSE;
}
else
{
return BChangeJson(wstrTemp);
}


return TRUE;
}
};


#endif

代码文件在附件中