标签:
/***************************************************************************************************** * Qt jsoncpp 对象拷贝、删除、函数调用 demo * 声明: * 本程序主要就是为了验证jsoncpp中Json::Value对象中的append、等号赋值是拷贝内容,还是赋值指针, * 就测试而言,从目前的情况来看应该是拷贝内容,在下面checkAndRemoveAppName()得到了验证。 * * 2015-11-15 晴 深圳 南山平山村 曾剑锋 ****************************************************************************************************/ #include <QCoreApplication> #include "json/json.h" #include <QString> #include <QFile> #include <QDebug> bool checkAndRemoveAppName(Json::Value *oldRoot, Json::Value *newRoot, QString name) { bool ret = false; Json::Value programs = ((*oldRoot)["main_menu"])["apps"]; Json::Value newPrograms; int i = 0; for ( i = 0; i < programs.size(); i++ ) { if ( QString::fromStdString(programs[i]["Name"].asString()).trimmed().compare(name.trimmed()) == 0 ) { ret = true; continue; } // 合成新的数据并加入新json数组对象中 Json::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; return ret; } #define jsonFile "F:\/QT\/json.txt" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 检查文件是否存在 QFile jsonFileObject("F:/QT/json.txt"); if ( !jsonFileObject.exists() ) { qDebug() << "Please check json.txt file already exist." << endl; return -1; } // 打开并读取文件 QString jsonString; if ( jsonFileObject.open(QIODevice::ReadOnly | QIODevice::Text) ) { jsonString.append(jsonFileObject.readAll()); jsonFileObject.close(); } else { qDebug() << "Open json file failt." << endl; return -1; } // 解析json数据 Json::Value root; Json::Reader reader; bool parsingSuccessful = reader.parse(jsonString.toStdString(), root); if ( !parsingSuccessful ) { qDebug() << "Failed to parse configure.\n "; return -1; } // 测试获取json数据 Json::Value programs = root["main_menu"]["apps"]; qDebug() << "programs size: " << programs.size(); // 这里的获取数据数据索引后面一定要加一个小写的u,目前还不知道具体的原因 std::cout << "Exec: " << programs[0u]["Exec"].asString() << std::endl; Json::Value outRoot; checkAndRemoveAppName(&root, &outRoot, "Information"); // 生成写入文件的字符串 Json::StyledWriter writer; std::string outputJsonstring = writer.write(outRoot); std::cout << outputJsonstring << std::endl; return a.exec(); } /** * json.txt * { * "main_menu" : { * "apps" : [ * { * "Description_Link" : -1, * "Exec" : "/usr/share/web/lighttpd/webpages/shell/browser.sh http://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/network.sh", * "Icon" : "apps/images/network-icon.png", * "Lock" : -1, * "Name" : "Network", * "Order" : 999, * "ProgramType" : "gui", * "Type" : "application" * } * ] * } */
标签:
原文地址:http://www.cnblogs.com/zengjfgit/p/4965190.html