{
"result": "no",
"shuju": [
{
"name": 1,
"info": "cc",
"price": "cc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 3,
"info": "ccc",
"price": "ccc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 4,
"info": "a",
"price": "0cc175b9c0f1b6a831c399e269772661",
"sdate": "None",
"edate": "None",
"id": null
},
{
"name": 12,
"info": "e",
"price": "e1671797c52e15f763380b45e841ec32",
"sdate": "None",
"edate": "None",
"id": null
}
],
"datas": {
"code": 1,
"data": [
{
"name": 1,
"info": "cc",
"price": "cc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 3,
"info": "ccc",
"price": "ccc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 4,
"info": "a",
"price": "0cc175b9c0f1b6a831c399e269772661",
"sdate": "None",
"edate": "None",
"id": null
},
{
"name": 12,
"info": "e",
"price": "e1671797c52e15f763380b45e841ec32",
"sdate": "None",
"edate": "None",
"id": null
}
]
}
}
#include "register.h"
#include <QString>
struct Data
{
QString code;
QJsonArray shuju;
};
Register::Register(QWidget *parent) : QDialog(parent)
{
//放到成员变量里
// QLineEdit* username;
// QLineEdit* password;
// QLineEdit* password2;
QPushButton* r;
QVBoxLayout* lay = new QVBoxLayout(this);
lay->addWidget(username= new QLineEdit);
lay->addWidget(password=new QLineEdit);
lay->addWidget(password2=new QLineEdit);
lay->addWidget( r= new QPushButton("注册"));
username->setPlaceholderText("请输入用户明");
password->setPlaceholderText("请输入密码");
password2->setPlaceholderText("密码确认");
this->setWindowTitle("注册");
connect(r,SIGNAL(clicked()),this,SLOT(slotReg()));
//窗口释放 对象也释放掉 +上this 是这意思
man = new QNetworkAccessManager(this);
connect(man,SIGNAL(finished(QNetworkReply*)),this,SLOT(slotNetworkApply(QNetworkReply*)));
}
void Register::slotReg()
{
QString username = this->username->text();
QString password = this->password->text();
QString password2 = this->password2->text();
if(password!=password2)
{
QMessageBox::warning(NULL,"注册失败","密码输入错误");
return;
}
//把注册信息提交到服务器 libcurl
QNetworkRequest req;
QString url=QString("http://%1:%2/reg").arg(SERVER_IP).arg(SERVER_PORT);
//下面这种拼接必须同类型
//QString temp =QString("http://%1:%2/reg").arg(SERVER_IP,SERVER_PORT);
//QUrl url1(temp);
req.setUrl(url);
//gaosu fu wuqi shi json geshi
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
// req.setUrl();
//req.setUrl(&(QUrl(QString("http://%1/user").arg(SERVER_IP))));
//req.setUrl(QUrl(QString("http://%1").arg(SERVER_IP)));
//man->post();
/*
username:xxx
passwd:yyy
*/
QJsonObject obj;
obj.insert("username",username);
obj.insert("password",password);
QByteArray data = QJsonDocument(obj).toJson();
man->post(req,data);
//man->post()
}
void Register::slotNetworkApply(QNetworkReply *reply )
{
if(reply->error()!=QNetworkReply::NoError)
{
QMessageBox::warning(this,"注册失败","网络错误");
return ;
}
// QByteArray data = reply->readAll();
// QJsonDocument doc =QJsonDocument::fromJson(data);
// QJsonObject obj = doc.object();
// QString result = obj.value("result").toString();
QJsonObject resp;
QByteArray data1 = reply->readAll();
resp = QJsonDocument::fromJson(data1).object();
QString result = resp.value("result").toString();
Data g_data;
if(result=="ok")
{
QMessageBox::warning(this,"注册成功","尽情享用");
accept();//关闭对话框
}
else
{
qDebug()<<result<<endl;//no
g_data.shuju = resp.value("shuju").toArray();
qDebug()<<g_data.shuju<<endl;
/*
QJsonArray([{"edate": "None","id": null,"info": "cc","name": 1,"price": "cc","sdate": "0"},
{"edate": "None","id": null,"info": "ccc","name": 3,"price": "ccc","sdate": "0"},
{"edate": "None","id": null,"info": "a","name": 4,"price": "0cc175b9c0f1b6a831c399e269772661","sdate": "None"},
{"edate": "None","id": null,"info": "e","name": 12,"price": "e1671797c52e15f763380b45e841ec32","sdate": "None"}])
*/
//qDebug()<<g_data.data.code<<endl;
QJsonArray courses = g_data.shuju;
qDebug()<<courses.size()<<endl;//4
for(int i=0; i<courses.size(); ++i)
{
QJsonObject course = courses.at(i).toObject();
//QJsonArray course = courses.at(i);
qDebug()<<course<<endl;
//QJsonObject({"edate": "None","id": null,"info": "ccc","name": 3,"price": "ccc","sdate": "0"})
if(course.contains("name"))
{
qDebug()<<"have name";
}
QJsonValue temp1 =course.take("name");
int temp2 = temp1.toInt();
qDebug()<<"name="<<temp2<<endl;
QString name = course.take("name").toString();
//course.take()
int id = course.value("id").toInt();
QString info = course.value("info").toString();
qDebug()<<"name="<<name<<endl;
//
// this->coursesList->coursesWidget->addItem(name);
}
QString info = QString("服务器返回错误:%1,请联系技术人员").arg(resp.value("code").toString());
QMessageBox::warning(this,"注册失败",info);
}
}
python 返回格式
如下
{"result": "no", "shuju": [{"name": 1, "info": "dsb12", "price": "c4ca4238a0b923820dcc509a6f75849b", "sdate": "None", "edate": "None", "id": null}, {"name": 2, "info": "d", "price": "8fa14cdd754f91cc6554c9e71929cce7", "sdate": "None", "edate": "None", "id": null}, {"name": 3, "info": "fk", "price": "b25ffa68ad761f8578cc61700c0140ed", "sdate": "None", "edate": "None", "id": null}, {"name": 4, "info": "hurry", "price": "46c48bec0d282018b9d167eef7711b2c", "sdate": "None", "edate": "None", "id": null}], "datas": {"code": 1, "data": [{"name": 1, "info": "dsb12", "price": "c4ca4238a0b923820dcc509a6f75849b", "sdate": "None", "edate": "None", "id": null}, {"name": 2, "info": "d", "price": "8fa14cdd754f91cc6554c9e71929cce7", "sdate": "None", "edate": "None", "id": null}, {"name": 3, "info": "fk", "price": "b25ffa68ad761f8578cc61700c0140ed", "sdate": "None", "edate": "None", "id": null}, {"name": 4, "info": "hurry", "price": "46c48bec0d282018b9d167eef7711b2c", "sdate": "None", "edate": "None", "id": null}]}}
=================================================================
qt连续操作处理
下面先是返回的结果
Starting /home/abcd/qtcode/live/build-Project-Desktop-Debug/QuickPlayer2/QuickPlayer2...
"no"
QJsonValue(array, QJsonArray([{"edate": "None","id": null,"info": "cc","name": 1,"price": "cc","sdate": "0"},{"edate": "None","id": null,"info": "ccc","name": 3,"price": "ccc","sdate": "0"},{"edate": "None","id": null,"info": "a","name": 4,"price": "0cc175b9c0f1b6a831c399e269772661","sdate": "None"},{"edate": "None","id": null,"info": "e","name": 12,"price": "e1671797c52e15f763380b45e841ec32","sdate": "None"}]) )
4
1
代码再下面 头文件 也在下面 这里写的补全
#include "register.h"
#include <QString>
struct Data
{
QString code;
QJsonArray shuju;
QJsonObject shuju1;
};
Register::Register(QWidget *parent) : QDialog(parent)
{
//放到成员变量里
// QLineEdit* username;
// QLineEdit* password;
// QLineEdit* password2;
QPushButton* r;
QVBoxLayout* lay = new QVBoxLayout(this);
lay->addWidget(username= new QLineEdit);
lay->addWidget(password=new QLineEdit);
lay->addWidget(password2=new QLineEdit);
lay->addWidget( r= new QPushButton("注册"));
username->setPlaceholderText("请输入用户明");
password->setPlaceholderText("请输入密码");
password2->setPlaceholderText("密码确认");
this->setWindowTitle("注册");
connect(r,SIGNAL(clicked()),this,SLOT(slotReg()));
//窗口释放 对象也释放掉 +上this 是这意思
man = new QNetworkAccessManager(this);
connect(man,SIGNAL(finished(QNetworkReply*)),this,SLOT(slotNetworkApply(QNetworkReply*)));
}
void Register::slotReg()
{
QString username = this->username->text();
QString password = this->password->text();
QString password2 = this->password2->text();
if(password!=password2)
{
QMessageBox::warning(NULL,"注册失败","密码输入错误");
return;
}
//把注册信息提交到服务器 libcurl
QNetworkRequest req;
QString url=QString("http://%1:%2/reg").arg(SERVER_IP).arg(SERVER_PORT);
//下面这种拼接必须同类型
//QString temp =QString("http://%1:%2/reg").arg(SERVER_IP,SERVER_PORT);
//QUrl url1(temp);
req.setUrl(url);
//gaosu fu wuqi shi json geshi
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
// req.setUrl();
//req.setUrl(&(QUrl(QString("http://%1/user").arg(SERVER_IP))));
//req.setUrl(QUrl(QString("http://%1").arg(SERVER_IP)));
//man->post();
/*
username:xxx
passwd:yyy
*/
QJsonObject obj;
obj.insert("username",username);
obj.insert("password",password);
QByteArray data = QJsonDocument(obj).toJson();
man->post(req,data);
//man->post()
}
void Register::slotNetworkApply(QNetworkReply *reply )
{
if(reply->error()!=QNetworkReply::NoError)
{
QMessageBox::warning(this,"注册失败","网络错误");
return ;
}
// QByteArray data = reply->readAll();
// QJsonDocument doc =QJsonDocument::fromJson(data);
// QJsonObject obj = doc.object();
// QString result = obj.value("result").toString();
QJsonObject resp;
QByteArray data1 = reply->readAll();
resp = QJsonDocument::fromJson(data1).object();
QString result = resp.value("result").toString();
Data g_data;
if(result=="ok")
{
QMessageBox::warning(this,"注册成功","尽情享用");
accept();//关闭对话框
}
else
{
qDebug()<<result<<endl;//no
g_data.shuju1 = resp.value("datas").toObject();
qDebug()<<g_data.shuju1.value("data")<<endl;
qDebug()<<g_data.shuju1.value("data").toArray().size()<<endl;
//QJsonArray temp11 = g_data.shuju.value("data");
//qDebug()<<temp11<<endl;
qDebug()<<g_data.shuju1.value("data").toArray().at(0).toObject().take("name").toInt()<<endl;
QString info = QString("服务器返回错误:%1,请联系技术人员").arg(resp.value("code").toString());
QMessageBox::warning(this,"注册失败",info);
}
}
{
"result": "no",
"shuju": [
{
"name": 1,
"info": "cc",
"price": "cc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 3,
"info": "ccc",
"price": "ccc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 4,
"info": "a",
"price": "0cc175b9c0f1b6a831c399e269772661",
"sdate": "None",
"edate": "None",
"id": null
},
{
"name": 12,
"info": "e",
"price": "e1671797c52e15f763380b45e841ec32",
"sdate": "None",
"edate": "None",
"id": null
}
],
"datas": {
"code": 1,
"data": [
{
"name": 1,
"info": "cc",
"price": "cc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 3,
"info": "ccc",
"price": "ccc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 4,
"info": "a",
"price": "0cc175b9c0f1b6a831c399e269772661",
"sdate": "None",
"edate": "None",
"id": null
},
{
"name": 12,
"info": "e",
"price": "e1671797c52e15f763380b45e841ec32",
"sdate": "None",
"edate": "None",
"id": null
}
]
}
}
头文件
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonValue>
分开多步骤循环
{
"result": "no",
"shuju": [
{
"name": 1,
"info": "cc",
"price": "cc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 3,
"info": "ccc",
"price": "ccc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 4,
"info": "a",
"price": "0cc175b9c0f1b6a831c399e269772661",
"sdate": "None",
"edate": "None",
"id": null
},
{
"name": 12,
"info": "e",
"price": "e1671797c52e15f763380b45e841ec32",
"sdate": "None",
"edate": "None",
"id": null
}
],
"datas": {
"code": 1,
"data": [
{
"name": 1,
"info": "cc",
"price": "cc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 3,
"info": "ccc",
"price": "ccc",
"sdate": "0",
"edate": "None",
"id": null
},
{
"name": 4,
"info": "a",
"price": "0cc175b9c0f1b6a831c399e269772661",
"sdate": "None",
"edate": "None",
"id": null
},
{
"name": 12,
"info": "e",
"price": "e1671797c52e15f763380b45e841ec32",
"sdate": "None",
"edate": "None",
"id": null
}
]
}
}
#include "register.h"
#include <QString>
struct Data
{
QString code;
QJsonArray shuju;
QJsonObject shuju1;
};
Register::Register(QWidget *parent) : QDialog(parent)
{
//放到成员变量里
// QLineEdit* username;
// QLineEdit* password;
// QLineEdit* password2;
QPushButton* r;
QVBoxLayout* lay = new QVBoxLayout(this);
lay->addWidget(username= new QLineEdit);
lay->addWidget(password=new QLineEdit);
lay->addWidget(password2=new QLineEdit);
lay->addWidget( r= new QPushButton("注册"));
username->setPlaceholderText("请输入用户明");
password->setPlaceholderText("请输入密码");
password2->setPlaceholderText("密码确认");
this->setWindowTitle("注册");
connect(r,SIGNAL(clicked()),this,SLOT(slotReg()));
//窗口释放 对象也释放掉 +上this 是这意思
man = new QNetworkAccessManager(this);
connect(man,SIGNAL(finished(QNetworkReply*)),this,SLOT(slotNetworkApply(QNetworkReply*)));
}
void Register::slotReg()
{
QString username = this->username->text();
QString password = this->password->text();
QString password2 = this->password2->text();
if(password!=password2)
{
QMessageBox::warning(NULL,"注册失败","密码输入错误");
return;
}
//把注册信息提交到服务器 libcurl
QNetworkRequest req;
QString url=QString("http://%1:%2/reg").arg(SERVER_IP).arg(SERVER_PORT);
//下面这种拼接必须同类型
//QString temp =QString("http://%1:%2/reg").arg(SERVER_IP,SERVER_PORT);
//QUrl url1(temp);
req.setUrl(url);
//gaosu fu wuqi shi json geshi
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
// req.setUrl();
//req.setUrl(&(QUrl(QString("http://%1/user").arg(SERVER_IP))));
//req.setUrl(QUrl(QString("http://%1").arg(SERVER_IP)));
//man->post();
/*
username:xxx
passwd:yyy
*/
QJsonObject obj;
obj.insert("username",username);
obj.insert("password",password);
QByteArray data = QJsonDocument(obj).toJson();
man->post(req,data);
//man->post()
}
void Register::slotNetworkApply(QNetworkReply *reply )
{
if(reply->error()!=QNetworkReply::NoError)
{
QMessageBox::warning(this,"注册失败","网络错误");
return ;
}
QJsonObject resp;
QByteArray data1 = reply->readAll();
resp = QJsonDocument::fromJson(data1).object();
QString result = resp.value("result").toString();
Data g_data;
if(result=="ok")
{
QMessageBox::warning(this,"注册成功","尽情享用");
accept();//关闭对话框
}
else
{
//先用对象接最外层 在获取想要 的里面那层 转换 成 toArray 最后再 遍历 遍历的时候 把里面的那层在转成QJsonObject 再用value或者take去获取 注意类型 否则的话 取不到为""
qDebug()<<result<<endl;//no
g_data.shuju1 = resp.value("datas").toObject();
qDebug()<<g_data.shuju1<<endl;
QJsonArray temp11 = g_data.shuju1.value("data").toArray();
qDebug()<<temp11<<endl;
qDebug()<<temp11.size()<<endl;
int k;
for(k=0;k<temp11.size();k++)
{
QJsonObject course = temp11.at(k).toObject();
int name = course.value("name").toInt();
qDebug()<<name;
}
QString info = QString("服务器返回错误:%1,请联系技术人员").arg(resp.value("code").toString());
QMessageBox::warning(this,"注册失败",info);
}
}
Starting /home/abcd/qtcode/live/build-Project-Desktop-Debug/QuickPlayer2/QuickPlayer2...
"no"
QJsonObject({"code": 1,"data": [{"edate": "None","id": null,"info": "cc","name": 1,"price": "cc","sdate": "0"},{"edate": "None","id": null,"info": "ccc","name": 3,"price": "ccc","sdate": "0"},{"edate": "None","id": null,"info": "a","name": 4,"price": "0cc175b9c0f1b6a831c399e269772661","sdate": "None"},{"edate": "None","id": null,"info": "e","name": 12,"price": "e1671797c52e15f763380b45e841ec32","sdate": "None"}]})
QJsonArray([{"edate": "None","id": null,"info": "cc","name": 1,"price": "cc","sdate": "0"},{"edate": "None","id": null,"info": "ccc","name": 3,"price": "ccc","sdate": "0"},{"edate": "None","id": null,"info": "a","name": 4,"price": "0cc175b9c0f1b6a831c399e269772661","sdate": "None"},{"edate": "None","id": null,"info": "e","name": 12,"price": "e1671797c52e15f763380b45e841ec32","sdate": "None"}])
4
1
3
4
12