cyley cyley - 1 year ago 156
C++ Question

Get the Output of Qstring in Json Array in qt c++

I have Written a code to get qbytearray output to Qstring list and then splited it using delimeter ',' , in qt c++

Now i need to added it into Json array

Output I am getting is

"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",


Expected output is

{
"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"
}


Now I need to get Expected output Where Starting with every node It should display in the next line in array?
I need to create Json array, Then for eachline , create a json object and then split them insert that in Json object and inser that json object in jsonarray, and return that jsonarray to json . How is this poosible? Can any one help?

Following is the code I have written then after that How to creatr and isert it in 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;
}


Pseudo code Written for What ouput I want is

ja = QJsonArray();
for (each line)
{
QJsonObect oj;
split("token")
for (each token in line)
oj.insert(descriptions, data)

ja.insert(oj)
}
return ja.toJson


How will I this Implement in my actual code.?

Following is my utils.cpp file

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


This is where mu Json is inserted

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


}

Answer Source

Finally I got the Expected output in QJson array:

      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);

Output obtained in Json Format:

     "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"
    }
],
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download