/***************************************************************************************************** * Qt jsoncpp object Copy, delete, function call demo * Declaration: * This program is mainly to verify the append in the Json::value object in the Jsoncpp, the Equal value assignment is the copy content, or the assignment pointer, * In terms of testing, from the current situation to See should be copy content, below Checkandremoveappname () is verified. * 2015-11-15 Sunny Shenzhen Nanshan Ping Shan village Zengjianfeng ********************************************** ******************************************************/#include<QCoreApplication>#include"Json/json.h"#include<QString>#include<QFile>#include<QDebug>BOOLCheckandremoveappname (Json::value *oldroot, Json::value *Newroot, QString name) { BOOLRET =false; Json::value Programs= ((*oldroot) ["Main_Menu"])["Apps"]; Json::value Newprograms; inti =0; for(i =0; I < programs.size (); i++ ) { if(Qstring::fromstdstring (programs[i]["Name"].asstring ()). trimmed (). Compare (Name.trimmed ()) = =0) {ret=true; Continue; } //synthesizing new data and adding new JSON array objectsJson::value NewItem; newitem["Name"] = programs[i]["Name"]; newitem["Order"] = programs[i]["Order"]; newitem["Icon"] = programs[i]["Icon"]; newitem["Type"] = programs[i]["Type"]; newitem["Exec"] = programs[i]["Exec"]; newitem["Programtype"] = programs[i]["Programtype"]; newitem["Description_link"] = programs[i]["Description_link"]; newitem["Lock"] = programs[i]["Lock"]; Newprograms.append (NewItem); } Json::value Newapps; newapps["Apps"] =Newprograms; (*newroot) ["Main_Menu"] =Newapps; returnret;}#defineJsonfile "F:\/qt\/json.txt"intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); //Check if the file existsQFile Jsonfileobject ("F:/qt/json.txt"); if( !jsonfileobject.exists ()) {Qdebug ()<<"Please check json.txt file already exist."<<Endl; return-1; } //Open and read a fileQString jsonstring; if(Jsonfileobject.open (Qiodevice::readonly |Qiodevice::text)) {Jsonstring.append (Jsonfileobject.readall ()); Jsonfileobject.close (); } Else{qdebug ()<<"Open json file failt."<<Endl; return-1; } //parsing JSON dataJson::value Root; Json::reader Reader; BOOLParsingsuccessful =Reader.parse (Jsonstring.tostdstring (), root); if( !parsingsuccessful) {Qdebug ()<<"Failed to parse configure.\n"; return-1; } //testing for JSON dataJson::value programs = root["Main_Menu"]["Apps"]; Qdebug ()<<"Programs Size:"<<programs.size (); //here to get Data data index must be added a lowercase u, currently do not know the specific reasonsStd::cout <<"Exec:"<< programs[0u]["Exec"].asstring () <<Std::endl; Json::value Outroot; Checkandremoveappname (&root, &outroot,"Information"); //generate a string to write to a filejson::styledwriter Writer; STD::stringOutputjsonstring =Writer.write (outroot); Std::cout<< outputjsonstring <<Std::endl; returna.exec ();}/** * json.txt * {* "Main_Menu": {* "apps": [* {* "Description_link":-1, * "Exec": "/usr/share/web/lighttpd/webpages/shell/browser.shhttp://processors.wiki.ti.com", *" Icon ":" Apps/images/web-icon.png ", *" lock ":-1, *" Name ":" Browser ", * "Order": 999, * "Programtype": "GUI", * "Type": "Application" *}, * {* "Description_link":-1, * "Exec": "/usr/share/web/lighttpd/webpages/shell/netwo Rk.sh ", *" Icon ":" Apps/images/network-icon.png ", *" lock ":-1, *" Name ":" Netwo RK ", *" Order ": 999, *" Programtype ":" GUI ", *" Type ":" Application "* } * ] * } */
Qt jsoncpp object Copy, delete, function call Demo