gpt4 book ai didi

c++ - 在qt c++中获取Json数组中Qstring的输出

转载 作者:行者123 更新时间:2023-11-28 05:18:36 25 4
gpt4 key购买 nike

我已经编写了一个代码来将 qbytearray 输出到 Qstring 列表,然后在 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 对象中,并将该 json 对象插入到 jsonarray 中,然后将该 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());
}

最佳答案

您可以修改以下代码:

#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());
}
}

关于c++ - 在qt c++中获取Json数组中Qstring的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42017609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com