1.QJsonObject和QByteArray的互相转换
可以将QJsonObject与QByteArray互相转化,以便网络编程时客户端与服务器之间的通信
1.1 QJsonObject转为QByteArray
QJsonObject json;QByteArray ary;QJsonDocument doc(json);ary= doc.toJson();
1.2 QByteArray转为QJsonObject
QByteArray ary;QJsonObject json;json = QJsonDocument::fromJson(ary).object();
2.HTTP操作
2.1 get
m_httpClient 是成员变量void Httptest::httpTest(){m_httpClient.get("http://122.51.80.50:8000/pump/").queryParam("pump_code", "001").onResponse([](QByteArray result){QJsonObject json;json = QJsonDocument::fromJson(result).object();// QJsonArray array = json.value("results").toArray();// qDebug() << "Result: " << array[1].toObject()["pump_code"].toString();qDebug() << "result " << json;}).onError([](QString errorStr){qDebug()<<"Error: "<<errorStr;}).exec();}
获取到的数据
2.2 put
void Httptest::httpPut(){QJsonObject content;content.insert("fluid_flow", "20");content.insert("mod_by", "3131f40fcb1b41a9a5bca7b41689b85a");qDebug() << "content = " << content;m_httpClient.put("http://122.51.80.50:8000/pump/1d3506ac8e0348879f9f483441365ad5/").header("content-type", "application/json").body(content).onResponse([](QByteArray result){qDebug() << "成功";//qDebug() << result;}).onError([](QString errorStr){qDebug()<<"Error: "<<errorStr;}).exec();}
代码如下:
webhttp.rar
2。3 POST
bool MyHttp::sendSensorConfig(){//没有网络就结束//插入设备编号QJsonObject content;content.insert("equipment_code", getEquipmentCode());content.insert("sensor_string", getSensorName());content.insert("high_sensor_threshold_string", getSensorThread(high));content.insert("down_sensor_threshold_string", getSensorThread(low));content.insert("sensor_code_string", getSensorCode());content.insert("equip_person", setting.getSetting("equipment", "configPerson"));QString sensorUrl = QString("http://%1:8000/equipment/").arg(getUrl());m_httpClient.post(sensorUrl).header("content-type", "application/json").body(content).onResponse([=](QByteArray result){Q_UNUSED(result);// qDebug() << "传感器配置成功";correct = true;}).onError([=](QString errorStr){correct = false;qDebug() << errorStr;}).block().timeout(2000).exec();return correct;}
2. JSON操作
包含头文件
#include <QJsonDocument>#include <QJsonObject>#include <QJsonArray>
获取到的JSON数据
({"count":1,"results":[{"create_by":"75bd4fd79e48448ebae23c905973a742","create_time":"2021-01-28","equipment_code":"20210128w","fluid_flow":"35(已修改)","mod_by":"3131f40fcb1b41a9a5bca7b41689b85a","mod_time":"2021-02-04","note":"测试","pump_code":"001","pump_id":"1d3506ac8e0348879f9f483441365ad5","pump_name":"测试泵1号","status":"1"}]})
一般获得http的数据是QByteArray
先转换为JSON
//result是如上的代码QJsonObject json;json = QJsonDocument::fromJson(result).object();//取值 取result的值 返回值是QJsonValueQJsonValue value = json.value("results");//同上一样QJsonValue value = json["results"];//打印为结果1qDebug() << "array " << value;
结果1
QJsonValue(array,QJsonArray([{"create_by":"75bd4fd79e48448ebae23c905973a742","create_time":"2021-01-28","equipment_code":"20210128w","fluid_flow":"20","mod_by":"3131f40fcb1b41a9a5bca7b41689b85a","mod_time":"2021-02-20","note":"测试","pump_code":"001","pump_id":"1d3506ac8e0348879f9f483441365ad5","pump_name":"测试泵1号","status":"1"}])
注意返回的是一个QJsonArray,我们需要
继续从上述取值
我们需要先转换为QJsonArray然后转换为value或者【】取值
//QJsonArrayQJsonArray value = json["results"].toArray();// QJsonValue(object, QJsonObject... QJsonValue(string, "测试泵1号")QJsonValue value2 = value[0].toObject().value("pump_name");//转为字符串 "测试泵1号"QString value3 = value2.toString();
遍历
数组遍历 foreach
QJsonObject json;json = QJsonDocument::fromJson(result).object();QJsonArray value = json["data"].toArray();int i = 0;foreach (QJsonValue test, value) {qDebug() << "test = " << test;qDebug() << "i = " << i++;}
for循环
QJsonObject json;json = QJsonDocument::fromJson(result).object();QJsonArray value = json["data"].toArray();QJsonArray::iterator iteratorArray = value.begin();for (int i = 0; i < value.size(); i++) {qDebug() << iteratorArray[i];qDebug() << "i = " << i;}

