基于yyjson封装的JSON序列化和反序列化C类库可适用于低版本C便于在多数C环境使用大部分JSON解析和创建功能已封装本项目地址https://gitcode.com/bitera/yyjson_for_cppyyjson项目地址 https://github.com/ibireme/yyjson// Read JSON string// #include JParse.h // Used for C{constchar*json{\name\:\Mash\,\star\:4,\hits\:[2,2,1,3]};// Create a parse object, parse JSON string, support {...} or [...]JParsej_Parse(json);if(j_Parse.isObj()){// If it is an objectautoj_objj_Parse.getObj();if(j_obj){// Get root[name]printf(name: %s\n,j_obj-asChars(name));// Get root[star]printf(star: %d\n,j_obj-asInt(star));// Get root[hits]autoj_arrj_obj-getArr(hits);if(j_arr){// Traverse sub elementsfor(size_t i0;ij_arr-size();i){printf(hit %d: %d\n,(int)i,j_arr-asInt(i));}}// Or Or extract array elements to std::vectorint32_tstd::vectorint32_thits;j_Parse.asVal(hits,hits);printf(vector hits size: %d\n,(int)hits.size());// Or iterate over the array/* j_val* hits j_Parse.getVal(hits); size_t idx, max; j_val* hit; yyjson_arr_foreach(hits, idx, max, hit) { printf(hit %d: %d\n, (int)idx, (int)j_get_int(hit)); } //*/}}elseif(j_Parse.isArr()){// If it is an arrayautoj_arrj_Parse.getArr();if(j_arr){// Number of array elementsprintf(Array size: %d\n,(int)j_arr-size());//......}}}// Write JSON string// #include Create.h //Used for C{// Create a objectJCreate::Object j_Obj;// Set root[name] and root[star]j_Obj.add(name,Mash);j_Obj.add(star,4);// Set root[hits] with an arrayinthits_arr[]{2,2,1,3};j_Obj.add(hits,hits_arr,4);// Or vectorstd::vectorinthits_arr2{6,7,8,9};j_Obj.add(hits2,hits_arr2);// Set root[obj] with an objectautoj_obj1j_Obj.addObj(obj);if(j_obj1){j_obj1-add(name,Tom);j_obj1-add(age,18);}// Set root[arr] with an array, can add more different types of elementsautoj_arr1j_Obj.addArr(arr);if(j_arr1){j_arr1-add(1);j_arr1-add(2);j_arr1-add(3);j_arr1-add(true);j_arr1-addNull();j_arr1-add(hits_arr2);}JCreate::Array j_arr2;j_arr2.add(1);j_arr2.add(2);j_arr2.add(3);j_arr2.add(hits_arr2);// Set an Array object to j_Obj, can add more different types of elements.// This method is equivalent to j_Obj.addRaw(arr2, j_arr2.write().c_str());// It is only a copy of the serialized text of the incoming object.j_Obj.addRaw(arr2,j_arr2);// To stringstd::string str;boolwTruej_Obj.write(str);if(wTrue){printf(json: %s\n,str.data());}}
基于yyjson封装的JSON序列化和反序列化C++类库
发布时间:2026/5/27 9:00:16
基于yyjson封装的JSON序列化和反序列化C类库可适用于低版本C便于在多数C环境使用大部分JSON解析和创建功能已封装本项目地址https://gitcode.com/bitera/yyjson_for_cppyyjson项目地址 https://github.com/ibireme/yyjson// Read JSON string// #include JParse.h // Used for C{constchar*json{\name\:\Mash\,\star\:4,\hits\:[2,2,1,3]};// Create a parse object, parse JSON string, support {...} or [...]JParsej_Parse(json);if(j_Parse.isObj()){// If it is an objectautoj_objj_Parse.getObj();if(j_obj){// Get root[name]printf(name: %s\n,j_obj-asChars(name));// Get root[star]printf(star: %d\n,j_obj-asInt(star));// Get root[hits]autoj_arrj_obj-getArr(hits);if(j_arr){// Traverse sub elementsfor(size_t i0;ij_arr-size();i){printf(hit %d: %d\n,(int)i,j_arr-asInt(i));}}// Or Or extract array elements to std::vectorint32_tstd::vectorint32_thits;j_Parse.asVal(hits,hits);printf(vector hits size: %d\n,(int)hits.size());// Or iterate over the array/* j_val* hits j_Parse.getVal(hits); size_t idx, max; j_val* hit; yyjson_arr_foreach(hits, idx, max, hit) { printf(hit %d: %d\n, (int)idx, (int)j_get_int(hit)); } //*/}}elseif(j_Parse.isArr()){// If it is an arrayautoj_arrj_Parse.getArr();if(j_arr){// Number of array elementsprintf(Array size: %d\n,(int)j_arr-size());//......}}}// Write JSON string// #include Create.h //Used for C{// Create a objectJCreate::Object j_Obj;// Set root[name] and root[star]j_Obj.add(name,Mash);j_Obj.add(star,4);// Set root[hits] with an arrayinthits_arr[]{2,2,1,3};j_Obj.add(hits,hits_arr,4);// Or vectorstd::vectorinthits_arr2{6,7,8,9};j_Obj.add(hits2,hits_arr2);// Set root[obj] with an objectautoj_obj1j_Obj.addObj(obj);if(j_obj1){j_obj1-add(name,Tom);j_obj1-add(age,18);}// Set root[arr] with an array, can add more different types of elementsautoj_arr1j_Obj.addArr(arr);if(j_arr1){j_arr1-add(1);j_arr1-add(2);j_arr1-add(3);j_arr1-add(true);j_arr1-addNull();j_arr1-add(hits_arr2);}JCreate::Array j_arr2;j_arr2.add(1);j_arr2.add(2);j_arr2.add(3);j_arr2.add(hits_arr2);// Set an Array object to j_Obj, can add more different types of elements.// This method is equivalent to j_Obj.addRaw(arr2, j_arr2.write().c_str());// It is only a copy of the serialized text of the incoming object.j_Obj.addRaw(arr2,j_arr2);// To stringstd::string str;boolwTruej_Obj.write(str);if(wTrue){printf(json: %s\n,str.data());}}