在qt c++中获取Json数组中Qstring的输出
Posted
技术标签:
【中文标题】在qt c++中获取Json数组中Qstring的输出【英文标题】:Get the Output of Qstring in Json Array in qt c++ 【发布时间】:2017-02-03 05:50:01 【问题描述】:我已经编写了一个代码来将 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 数组,然后为 eachline 创建一个 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());
【问题讨论】:
Qt 知道 JSON 格式。 doc.qt.io/qt-5/json.html 你能解释一下我如何实现它吗? 有一个具体的例子! :) doc.qt.io/qt-5/qtcore-json-savegame-example.html 我试过了,但我无法弄清楚?你能帮我解决上面的例子吗 请指定您要执行的操作。你想把数据转换成 QByteArray 吗?还是使用 JSON 格式保存?如果你想保存它,你应该在你的伪代码中插入序列化部分。 【参考方案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());
【讨论】:
我试过这个不起作用。我的字符串也不是一个列表,它是 Qstring 您可以添加您尝试过的代码吗?然后我会尝试修复它。 我已经编辑了我之前的帖子并为我想要的输出添加了伪代码?您能帮我解决以下问题吗? @cyley “不起作用”是什么意思?如果这意味着你无法编译它,你必须粘贴代码而不是它的伪代码。【参考方案2】:最后我得到了 QJson 数组中的预期输出:
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"
],
【讨论】:
以上是关于在qt c++中获取Json数组中Qstring的输出的主要内容,如果未能解决你的问题,请参考以下文章