我已经编写了一个代码来将qbytearray输出到Qstring列表然后使用deimeter','来分割它。 ,在qt c ++中
现在我需要将它添加到Json数组
我得到的输出是
"diskinfo": "Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 420842713088, Name: C:, Size : 499875049472 Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: , Name: D:, Size : Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 324858568704, Name: E:, Size : 487687450624 Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: 0, Name: F:, Size : 553459712",
预期输出
{
"diskinfo": "Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 420842713088, Name: C:, Size : 499875049472
Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: , Name: D:, Size :
Node: ASHUTOSH-PC, Description: Local Fixed Disk, FreeSpace: 324858568704, Name: E:, Size : 487687450624
Node: ASHUTOSH-PC, Description: CD-ROM Disc, FreeSpace: 0, Name: F:, Size : 553459712"
}
现在我需要获得预期的输出从每个节点开始它应该显示在数组的下一行? 我需要创建一个Json数组,然后为每一行创建一个json对象,然后将它们分开,将它插入Json对象和jsonarray中的json对象,并将该jsonarray返回给json。这怎么可能?任何人都可以帮忙吗?
以下是我之后编写的代码,如何在Json
中创建并使用它QString Utils::getDiskSpace()
{
static QString diskSpaceCmd ( qgetenv("WINDIR") +
"\\system32\\wbem\\wmic logicaldisk get name, freespace, size, description /format:csv");
QProcess proc;
QByteArray qba;
QString out_str;
proc.start(diskSpaceCmd);
if(proc.waitForFinished(-1))
{
qba = proc.readAllStandardOutput();
qba = qba.trimmed();
qba = qba.replace('\r',' ');
QString myString = qba;
QStringList myStringList = myString.split("\n");
QStringList descriptions = myStringList[0].split(",");
descriptions[0] = descriptions[0].remove(0,
descriptions[0].indexOf(" ") + 1);
for(int index = 1;index < myStringList.length();index++)
{
QStringList data = myStringList[index].split(",");
QStringList out;
for(int ind_2 = 0; ind_2 < data.length(); ind_2++)
out.push_back(descriptions[ind_2] + ": " +data[ind_2]);
out_str += out.join(", ");
}
}
QString diskSpace(out_str);
return diskSpace;
}
为我想要的输出写的伪代码是
ja = QJsonArray();
for (each line)
{
QJsonObect oj;
split("token")
for (each token in line)
oj.insert(descriptions, data)
ja.insert(oj)
}
return ja.toJson
我将如何在实际代码中实现此功能。?
以下是我的utils.cpp文件
QString WctUtils::getDiskSpace( )
{
static QString diskSpaceCmd ( qgetenv("WINDIR")
+ "\\system32\\wbem\\wmic logicaldisk get name, freespace, size, description /format:csv");
QProcess proc;
QByteArray qba;
QString out_str;
proc.start(diskSpaceCmd);
if(proc.waitForFinished(-1))
{
qba = proc.readAllStandardOutput();
qba = qba.trimmed();
qba =qba.replace('\r',' ');
QString myString =qba;
QStringList myStringList = myString.split("\n");
QStringList descriptions = myStringList[0].split(",");
for(int index = 1;index < myStringList.length();index++)
{
QStringList data = myStringList[index].split(",");
QStringList out;
for(int ind_2 = 0; ind_2 < data.length(); ind_2++)
out.push_back(descriptions[ind_2] + ": " +data[ind_2]);
out_str += out.join(", ");
}
}
QString diskSpace(out_str);
return (diskSpace);
}
这是插入mu Json的地方
QJsonObject json;
if(isRemoteServiceManagerEnabled)
{
QLOG_INFO() << "windows service manager web service called to get start time";
json = JamManager::getServiceStartTimes();
json.insert(ask::HOST_UPTIME, WctUtils::getLastBootupTime());
json.insert(ask::DISK_INFO, WctUtils::getDiskSpace());
json.insert(ask::CPU_USAGE,WctUtils::getcpuUsage());
json.insert(ask::HOST, QHostInfo::localHostName());
}
QJsonDocument replyDoc = QJsonDocument(json);
aResponse.setHeader(ask::CONTENT_TYPE.toLocal8Bit(), al::JSON_HEADER.toLocal8Bit());
aResponse.write(replyDoc.toJson());
}
}
答案 0 :(得分:1)
您可以修改以下代码:
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
QStringList infos = {"foo", "foofoo", "foofoofoo"}; // you must change it.
// create a json array and fill it.
QJsonArray arr;
foreach(QString const& info, infos)
arr.append(info);
// create a object for converting.
QJsonObject obj;
obj["diskinfo"] = arr;
// save it.
QJsonDocument doc(obj);
QFile file("save.json");
if(file.open(QIODevice::WriteOnly))
file.write(doc.toJson());
输出(save.json):
{
"diskinfo": [
"foo",
"foofoo",
"foofoofoo"
]
}
编辑: 我将在您的第一个代码中实现上述代码。
void Utils::getDiskSpace(QString filename)
{
static QString const diskSpaceCmd ( qgetenv("WINDIR") +
"\\system32\\wbem\\wmic logicaldisk get name, freespace, size, description /format:csv");
QProcess proc;
proc.start(diskSpaceCmd);
if(proc.waitForFinished(-1))
{
QByteArray qba;
qba = proc.readAllStandardOutput();
qba = qba.trimmed();
qba = qba.replace('\r',' ');
QString myString(qba);
QStringList myStringList = myString.split("\n");
QStringList descriptions = myStringList[0].split(",");
descriptions[0] = descriptions[0].remove(0,
descriptions[0].indexOf(" ") + 1);
QJsonArray jsarr;
for(int index = 1;index < myStringList.length();index++)
{
QStringList data = myStringList[index].split(",");
QStringList out;
for(int ind_2 = 0; ind_2 < data.length(); ind_2++)
out.push_back(descriptions[ind_2] + ": " +data[ind_2]);
jsarr.append(out.join(", "));
}
// create a object for converting.
QJsonObject obj;
obj["diskinfo"] = jsarr;
// save it.
QJsonDocument doc(obj);
QFile file(filename);
if(file.open(QIODevice::WriteOnly))
file.write(doc.toJson());
}
}
答案 1 :(得分:0)
最后我得到了QJson数组中的Expected输出:
QJsonArray disk_array;
QStringList myStringList = myString.split("\n");
QStringList descriptions = myStringList[0].split(",");
for(int index = 1;index < myStringList.length();index++)
{
QStringList data = myStringList[index].split(",");
QJsonObject obj_disk;
for(int ind_2 = 0; ind_2 < data.length(); ind_2++)
{
obj_disk.insert(descriptions[ind_2],data[ind_2]);
}
disk_array.append(obj_disk);
}
return (disk_array);
以Json格式获得的输出:
"diskinfo": [
{
"Description": "Local Fixed Disk",
"FreeSpace": "418491035648",
"Name": "C:",
"Node": "ASHUTOSH-PC",
"Size ": "499875049472 "
},
{
"Description": "CD-ROM Disc",
"FreeSpace": "",
"Name": "D:",
"Node": "ASHUTOSH-PC",
"Size ": " "
},
{
"Description": "Local Fixed Disk",
"FreeSpace": "324668514304",
"Name": "E:",
"Node": "ASHUTOSH-PC",
"Size ": "487687450624 "
},
{
"Description": "CD-ROM Disc",
"FreeSpace": "0",
"Name": "F:",
"Node": "ASHUTOSH-PC",
"Size ": "553459712"
}
],