gpt4 book ai didi

php - QT5 : best efficiant way to "unserialise" array of keys/values from json result

转载 作者:行者123 更新时间:2023-11-29 07:32:36 25 4
gpt4 key购买 nike

我从QT开始,从php取回数据(发送mysql结果)是一场噩梦

PHP 端:

...
$rs = mysql_query( $sql );
while ($row = mysql_fetch_assoc($rs)) {
$result[] = $row;
}
echo json_encode(array('code'=>'OK', 'result' => $result));

女巫给出以下结果:

{"code":"OK","result":
[{"CCMGD_HOUR":"19980","CCMGD_DAY":"3"},
{"CCMGD_HOUR":"32400","CCMGD_DAY":"3"},
{"CCMGD_HOUR":"71100","CCMGD_DAY":"3"},
{"CCMGD_HOUR":"71700","CCMGD_DAY":"3"},
{"CCMGD_HOUR":"72000","CCMGD_DAY":"3"}]}

现在,QT 方面:

...
QNetworkReply* reply;
QString data = (QString) reply->readAll();
QJsonDocument d = QJsonDocument::fromJson(data.toUtf8());
QJsonObject jo = d.object();

QVariantHash hash = jo.toVariantHash();
QVariantList list = hash.value("result").toList();

但是之后???如果我擅长这一点,如何迭代 trought 行并提取键(CCMGD_HOUR 和 CCMGD_DAY)?

我找到了如何迭代:

foreach(const QVariant &item, result){
...//but item has 2 items for the row:
[0]
key "CCMGD_HOUR"
value "19980"
[1]
key "CCMGD_DAY"
value "3"

how to extract this ?
}

谢谢!

最佳答案

要从 Json 中提取值,请尝试此操作

QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
QJsonObject jo = document.object();
foreach (const QJSonValue& value, jo["result"].toArray()) {
QJsonObject obj = value.toObject();
qDebug() << "CCMGD_HOUR: " << obj["CCMGD_HOUR"].toInt();
qDebug() << "CCMGD_DAY: " << obj["CCMGD_DAY"].toInt();
}

关于php - QT5 : best efficiant way to "unserialise" array of keys/values from json result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32090863/

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