QT5.15 QT Qstring转QJson
//读取json格式文件
// QByteArray json = http_recv_str.toLatin1();//字符串转字节
QByteArray json = http_recv_str.toUtf8().data(); //信息带中文,则需要toUtf8()
//QByteArray json = http_recv_str.toLocal8Bit().data();//信息不带中文,toLocal8Bit
bool Dialog_Debug::QString2QJson(QString msg)
{
http_recv_str= msg;
// 读取文本中的 JSON 数据
QString Current_Path = QDir::currentPath();
//Current_Path = Current_Path+"/debug/config/test.json"; //读取json格式文件
// QFile file(Current_Path);
// file.open(QFile::ReadOnly);
//QByteArray json = file.readAll();
// file.close();
// QByteArray json = http_recv_str.toLatin1();//字符串转字节
QByteArray json = http_recv_str.toUtf8().data(); //信息带中文,则需要toUtf8()
//QByteArray json = http_recv_str.toLocal8Bit().data();//信息不带中文,toLocal8Bit
// 将 JSON 数据转换为 QJsonDocument 对象
QJsonDocument doc = QJsonDocument::fromJson(json);
if(!doc.isObject())
{// 不是 JSON 对象:JSON 字符串格式错误或无效
qDebug()<<"不是 JSON 对象:JSON 字符串格式错误或无效";
return false;
}
// 将 QJsonDocument 对象中的 JSON 数据转换为 JSON 对象
QJsonObject obj = doc.object();
// 获取 JSON 数据中的所有键
QStringList keys = obj.keys();
// 遍历键值对
for (int i = 0;i<keys.size();++i)
{
// 获取每一个 key
QString key = keys[i];
// 通过 key 获取对应的 value
QJsonValue value = obj.value(key);
if(value.isBool())
{// 值是一个 bool
qDebug()<<key<<":"<<value.toBool();
}
else if(value.isString())
{// 值是一个字符串
qDebug()<<key<<":"<<value.toString();
}
else if(value.isDouble())
{// 值是一个 double 或者 int(double 和 int 都用 isDouble)
qDebug()<<key<<":"<<value.toInt();
}
else if(value.isObject())
{// 值是一个 JSON 对象
qDebug()<<key<<":";
//直接通过已知的键去获取值
QJsonObject lotInfoObj = value.toObject();
QString waferxxSource = lotInfoObj["waferSxxxource"].toString();
QString lot_dexxvice = lotInfoObj["partNxxxame"].toString();
QString lot_paxxckage = lotInfoObj["pacxxxkage"].toString();
QString lot_stxxep_name = lotInfoObj["stepxxName"].toString();
QString lot_stxxate = lotInfoObj["state"].toString();
QString lot_rewxxork_value = lotInfoObj["Resexxxrved17"].toString();
qDebug()<<" "<<"waferSxxource:"<<waferxxSource;
qDebug()<<" "<<"lot_dxxevice:"<<lot_xxdevice;
ui->lineEdit_xxwafer_source->setText(waferSxxource);
ui->lineEdit_xxdevice->setText(lot_dxxevice);
ui->lineEdit_xxpackage->setText(lot_xxpackage);
ui->lineEdit_xxstep_name->setText(lot_sxxtep_name);
ui->lineEdit_xxstate->setText(lot_sxxtate);
ui->lineEdit_xxrework->setText(lot_reworkxx_value);
QString main_qty_str = lotInfoObj["maxxinQty"].toString();
QString reelxxSize = lotInfoObj["reexxlSize"].toString();
int temp_calc = main_qxxty_str.toInt();
temp_calc = temp_calc/reelxxSize.toInt();
ui->lineEdit_reels_total_qty->setText(QString::number(temp_calc));
}
/**
*string mainQty = jsonStr["data"]["mainQtyxx"];
string reelSize = jsonStr["data"]["reelSizexx"];
int temp_calc = Convert.ToInt32(mainQty) / Convert.ToInt32(reelSizexx);
reel_total_qty = temp_calc.ToString();
*
* */
else if (value.isArray())
{// 值是一个 JSON 数组
qDebug()<<key<<":";
QJsonArray phoneArray = value.toArray();
for (int i = 0;i<phoneArray.size();++i)
{// 遍历 JSON 数组中的每一个 JSON 对象
QJsonObject phoneObj = phoneArray[i].toObject();
QString waferSourcexx = phoneObj["waferSourcexx"].toString();
QString lot_devicexx = phoneObj["lot_devicexx"].toString();
qDebug()<<" "<<"number:"<<waferSourcexx;
qDebug()<<" "<<"type:"<<lot_devicexx;
}
}
}
}
// 构建 JSON 对象
QJsonObject json;
json.insert("Name", "Qt");
json.insert("From", 1991);
json.insert("Cross Platform", true);
// 构建 JSON 文档
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson;
————————————————
QJson转QString。该转换不会产生换行符
QString MainWindow::JsonToQstring(QJsonObject jsonObject)
{
QJsonDocument document;
document.setObject(jsonObject);
QByteArray simpbyte_array = document.toJson(QJsonDocument::Compact);
QString simpjson_str(simpbyte_array);
return simpjson_str;
}
// QString转QByteArray方法
QString str("hello");
QByteArray bytes = str.toUtf8(); // QString转QByteArray方法1
QString str("hello");
QByteArray bytes = str.toLatin1(); // QString转QByteArray方法2
//QByteArray转QString方法
//Qt5.3.2
QByteArray bytes("hello world");
QString string = bytes; // QByteArray转QString方法1
QByteArray bytes("hello world");
QString string;
string.prepend(bytes);// QByteArray转QString方法2
qDebug() << string;
QByteArray类同样不能以’\0’为结尾,比如
QByteArray bytes;
bytes.resize(5);
bytes[0] = '1';
bytes[1] = '2';
bytes[2] = '3';
bytes[3] = '\0';
bytes[4] = 'a';
cout << bytes << endl;