This is because JSON data in the HTTP protocol environment is required by the project for interaction. c/C ++ is used for local development. to reduce the workload, you need to find several open source libraries for support. HTTP curl. JSON looks like there are a lot of libraries to choose from. I have found a jsoncpp and a libjson library for merit-based admission.
Because the data source in JSON format is output in Java, keys in the object set in JSON do not use double quotation marks, and the parse in jsoncpp and libjson cannot succeed. then I asked my colleagues to test in the JSON mode in the boost library. the result still does not support keys without double quotation marks.
The expected data format of libjson is {"key": "value"}. Data provided by http: {key: Value}
Data sources are supported by third-party software. there is no room for modification, and there is no other way. I had to write such a function myself: implement a function. the function reads and parses JSON strings and adds the keys in all sets with double quotation marks. the requirements are as follows. the preferred function cannot interfere with double quotation marks and escape characters in JSON strings. second. arrya and object nesting and hybrid nesting in JSON must be supported. third, it cannot be disturbed by blank characters or other codes.
After analysis and debugging, a function is written to add the key in a JSON string with double quotation marks.
This function can be used with various C/C ++ versions of JSON to add and delete double quotation marks in JSON strings. The function is only used for JSON data without formatting.
// Libjsontest. cpp: defines the entry point of the console application. <Br/> // </P> <p> # include "stdafx. H "</P> <p> # include" libjson/libjson. H "<br/> # include" libjson_ext.h "</P> <p> # pragma comment (Lib," statlibjson. lib ") </P> <p> char * json_getobjtype (char type) <br/> {<br/>/* <br/> # define json_null '/0' <br/> # define json_string'/1' <br/> # define json_number '/2' <br/> # define json_bool'/3' <br/> # define json_array '/4' <br/> # define json_node'/5' */< br/> switch (type) <br/> {<B R/> case '/0': <br/> return "null"; <br/> case'/1': <br/> return "string "; <br/> case '/2': <br/> return "Number"; <br/> case'/3': <br/> return "bool "; <br/> case '/4': <br/> return "array"; <br/> case'/5': <br/> return "Node "; <br/> default: <br/> return "undefine "; <br/>}</P> <p> // exclude the quotation mark area and search for special characters. <br/> char * json_getcharaddr (char * Start, char * end, char key) <br/>{< br/> char INQ = '#'; <br/> char * star Tbak = start; <br/> while (start <End) <br/> {<br/> If (INQ = '#' & (* Start = '/''| * Start = '"') & (START = startbak | * (START-1 )! = '//') <Br/>{< br/> INQ = * start; // enter the guide area. <br/> Start ++; <br/> continue; <br/>}< br/> If (INQ! = '#' & * Start = INQ & * (START-1 )! = '//') <Br/>{< br/> INQ = '#'; // exit the cited area. <br/> Start ++; <br/> continue; <br/>}< br/> If (INQ = '#' & * Start = key) return start; // found. <br/> Start ++; <br/>}< br/> return NULL; <br/>}</P> <p> void * json_findnode (void * JSON, char * XPath) <br/>{< br/> char * _ XPath = XPath; <br/> char * xpathend = XPath + strlen (XPath ); <br/> char value [2048]; <br/> unsigned int valueindex = 0; <br/> void * ret = JSON; <br/> int ind = 0; <br/> while (_ x Path <= xpathend) <br/> {<br/> If (* _ XPath = '/' | * _ XPath = '/0' | _ XPath = xpathend) <br/>{< br/> If (valueindex! = 0) <br/>{< br/> // printf ("% s/n", value); <br/> char * valueend = value + valueindex; <br/> char * spkey = json_getcharaddr (value, valueend, ':'); <br/> char * spkeyold = value; <br/> If (spkey) <br/>{< br/> while (spkey) <br/>{< br/> * spkey = '/0'; <br/> spkey ++; <br/> ret =: json_get (Ret, spkeyold); <br/> // printf ("% s/n", spkeyold); <br/> If (! RET) return NULL; <br/> If (spkey = valueend) break; <br/> spkeyold = spkey; <br/> spkey = json_getcharaddr (spkeyold, valueend ,': '); <br/>}< br/> // printf ("% s/n", spkeyold, spkey ); <br/> // int size =: json_size (RET); <br/> // If (size = 0 ); // here the value is not OBJ or arr <br/> If (spkey = NULL) <br/>{< br/> ind = atoi (spkeyold ); <br/> // printf ("% s/n", spkeyold); <br/> ret =: json_at (Ret, IND); <br/> If (! RET) return NULL; <br/>}< br/> else <br/>{< br/> ind = atoi (value ); <br/> int size =: json_size (RET); <br/> If (size = 0) return NULL; // here the value is not OBJ or arr <br/> ret =: json_at (Ret, IND); <br/> If (! RET) return NULL; <br/>}< br/> memset (value, 0, valueindex); <br/> valueindex = 0; <br/>}< br/> else <br/>{< br/> value [valueindex] = * _ XPath; <br/> valueindex ++; <br/> If (valueindex> sizeof (value) <br/> return NULL; <br/>}< br/> _ XPath ++; <br/>}< br/> return ret; <br/>}</P> <p> // obtain the first non-blank character. <br/> char * json_getfirstcharaddr (char * Start, char * End) <br/>{< br/> while (start <End) <br/>{< br/> If (! Isspace (* Start) return start; <br/> Start ++; <br/>}< br/> return NULL; <br/>}< br/> // obtain the last non-blank character. <br/> char * json_getlastcharaddr (char * Start, char * End) <br/>{< br/> end --; <br/> while (end> = start) <br/>{< br/> If (! Isspace (* End) return end + 1; <br/> end ++; <br/>}< br/> return NULL; <br/>}</P> <p> bool json_formatjsonkey (char ** injson, char * injsonend, char ** outjson, char * outjsonmax, bool addq/* = true */, char split/* = '#' */) <br/> {// The function does not care about the value boundary. <br/> // escape: {[,: "']} <br/> // escape:/converted to:",',/,/, B, f, N, R, T, U <br/> // The value cannot contain ", /<br/> // The function determines in recursion that injson must contain only one value. <br/> char * _ injson = * injson; <br/> char * _ outjson = * outjson; </P> <p> char _ Split = '#'; // The default Delimiter is unbounded #. find the bounds, move out of bounds, and then return. if not. view the processing of the parent field. <br/> // The boundary cannot be found! Process by value. </P> <p> // removes the spaces before the first valid character <br/> while (_ injson <injsonend) // The loop condition is that the string is not processed completely. <br/>{< br/> If (isspace (* _ injson) // blank characters are not processed under any circumstances <br/> _ injson ++; <br/> else <br/> break; <br/>}< br/> If (_ injson = injsonend) return false; <br/> // confirm the delimiter. <br/> If (* _ injson = '[' | * _ injson = '{') <br/> _ Split = * _ injson; <br/> else <br/> _ Split = '! '; </P> <p> while (_ injson <injsonend) // The loop condition is that the string is not processed completely. <br/>{< br/> If (_ outjson> = outjsonmax) return false; <br/> // _ Split can only be set [,{,! <Br/> switch (_ Split) <br/> {<br/> case '[': <br/> // enters the array field. <br/> If (* _ injson = '[' | * _ injson = ',') <br/>{< br/> * _ outjson = * _ injson; <br/> _ injson ++; <br/> _ outjson ++; <br/> If (! Json_formatjsonkey (& _ injson, injsonend, & _ outjson, outjsonmax, addq, _ Split) <br/> return false; <br/> * injson = _ injson; <br/> * outjson = _ outjson; // the pointer jumps forward due to function execution. <br/>}< br/> else if (* _ injson = ']') <br/>{< br/> // outbound. enter the parent field. <br/> * _ outjson = * _ injson; <br/> _ injson ++; <br/> _ outjson ++; <br/> * injson = _ injson; <br/> * outjson = _ outjson; <br/> * _ outjson = '/0'; <br/> return true; // recursive return point. <br/>}< br/> else <br/> {<br/> _ injson + +; <Br/>}< br/> break; <br/> case '{': <br/> // enter the object field. <br/> If (* _ injson = '{' | * _ injson = ',') <br/>{< br/> * _ outjson = * _ injson; <br/> _ injson ++; <br/> _ outjson ++; <br/> // perform key processing <br/> char * TMP = json_getcharaddr (_ injson, injsonend ,':'); </P> <p> If (TMP = NULL) return false; // The Key boundary is not found. <br/> char * keyfirst = json_getfirstcharaddr (_ injson, TMP); <br/> char * keyend = json_getlastcharaddr (keyfirst, TMP); <br/> int tmplen = Key End-keyfirst; <br/> bool needq = * keyfirst! = '"'; <Br/> If (addq) <br/>{< br/> If (needq) <br/>{< br/> * _ outjson = '/"'; <br/> _ outjson ++; <br/>}< br/> memcpy (_ outjson, keyfirst, tmplen); <br/> _ outjson + = tmplen; <br/> _ injson = TMP; <br/> If (needq) <br/>{< br/> * _ outjson = '/"'; <br/> _ outjson ++; <br/>}< br/> else <br/> {<br/> If (! Needq) <br/>{< br/> keyfirst ++; <br/> tmplen-= 2; <br/>}< br/> memcpy (_ outjson, keyfirst, tmplen); <br/> _ outjson + = tmplen; <br/> _ injson = TMP; <br/>}< br/> // process the value <br/> * _ outjson = * _ injson; <br/> _ injson ++; <br/> _ outjson ++; <br/> If (! Json_formatjsonkey (& _ injson, injsonend, & _ outjson, outjsonmax, addq, _ Split) <br/> return false; <br/> * injson = _ injson; <br/> * outjson = _ outjson; // the pointer jumps forward due to function execution. <br/>}< br/> else if (* _ injson = '}') <br/>{< br/> // outbound. enter the parent field. <br/> * _ outjson = * _ injson; <br/> _ injson ++; <br/> _ outjson ++; <br/> * injson = _ injson; <br/> * outjson = _ outjson; <br/> * _ outjson = '/0'; <br/> return true; // recursive return point. <br/>}< br/> else <br/> {<br/> _ injson + +; <Br/>}< br/> break; <br/> case '! ': <Br/> switch (split) <br/> {// if it is determined that the current content is neither array nor obj. it is bounded by the parent content. <br/> case '[': <br/> If (* _ injson = ']' | * _ injson = ',') <br/> {<br/> // exit. enter the parent field. <br/> * _ outjson = * _ injson; <br/> * injson = _ injson; <br/> * outjson = _ outjson; <br/> return true; // recursive return point. <br/>}< br/> else <br/> {<br/> * _ outjson = * _ injson; <br/> _ injson ++; <br/> _ outjson ++; <br/> continue; <br/>}< br/> break; <br/> case '{': <br/> If (* _ injson = '}' | * _ injson = ',') <br/>{< br/> // exit. enter the parent field. <br/> * _ outjson = * _ injson; <br/> * injson = _ injson; <br/> * outjson = _ outjson; <br/> return true; // recursive return point. <br/>}< br/> else <br/> {<br/> * _ outjson = * _ injson; <br/> _ injson ++; <br/> _ outjson ++; <br/> continue; <br/>}< br/> break; <br/> default: <br/> return false; <br/>}< br/> break; <br/> default: <br/> return false; <br/>}< br/> return false; <br/>}</P> <p>