Recently, when using jsoncpp, it is necessary to determine whether the current string is the correct JSON format, but jsoncpp the string as the correct JSON data, resulting in an error when fetching
Add a validation method that verifies that the data is correct before conversion, before converting
1 BOOLIsjsonillegal (Const Char*jsoncontent)2 {3std::stack<Char>Jsonstr;4 Const Char*p =jsoncontent;5 CharStartchar = jsoncontent[0];6 CharEndChar =' /';7 BOOLIsObject =false;//Prevent {} {} from judging8 BOOLIsArray =false;//prevention of [] judgment9 Ten while(*p! =' /') One { AEndChar = *p; - Switch(*p) - { the Case '{': - if(!IsObject) - { -IsObject =true; + } - Elseif (Jsonstr.empty ())//object repeat into stack + { A return false; at } -Jsonstr.push ('{'); - Break; - Case '"': - if(Jsonstr.empty () | | jsonstr.top ()! ='"') - { inJsonstr.push (*p); - } to Else + { - Jsonstr.pop (); the } * Break; $ Case '[':Panax Notoginseng if(!IsArray) - { theIsArray =true; + } A Elseif (Jsonstr.empty ())//array repeat in stack the { + return false; - } $Jsonstr.push ('['); $ Break; - Case ']': - if(Jsonstr.empty () | | jsonstr.top ()! ='[') the { - return false;Wuyi } the Else - { Wu Jsonstr.pop (); - } About Break; $ Case '}': - if(Jsonstr.empty () | | jsonstr.top ()! ='{') - { - return false; A } + Else the { - Jsonstr.pop (); $ } the Break; the Case '\\'://escaped characters, skipping thep++; the Break; - default: in ; the } thep++; About } the the if(Jsonstr.empty ()) the { + //Make sure it's an object or an array, not a valid one. - Switch(Startchar)//Ensure that the symbol corresponds the {Bayi Case '{': the { the if(EndChar ='}') - { - return true; the } the return false; the } the Case '[': - { the if(EndChar =']') the { the return true;94 } the return false; the } the default:98 return false; About } - 101 return true;102 }103 Else104 { the return false;106 }107}
C + + Jsoncpp parse for JSON string parsing and conversion to complement JSON format validation