#include <QtNetwork>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
要注意的是心知天气接收的数据应该是Utf8
//天气请求
QString weatherReq = 你的请求连接;
//创建天气Socket对象
QTcpSocket *weatherSocket= new QTcpSocket(this);
//连接服务器
weatherSocket->connectToHost("api.seniverse.com",80);
//等待连接
if(weatherSocket->waitForConnected())
bool isWeatherServerConnect = true;
//发送请求
weatherSocket->write(weatherReq.toUtf8());
//获取数据 解析json内容
connect(weatherSocket,&QTcpSocket::readyRead,[=]()
{
QByteArray text = weatherSocket->readAll();//读取所有的数据
QJsonObject json = QJsonDocument::fromJson(text).object();//获取json 源码
if(json.find("results")!=json.end())
{
QString temp = json["results"].toArray()[0].toObject()["now"].toObject()["temperature"].toString();//获取温度
QString weather = json["results"].toArray()[0].toObject()["now"].toObject()["text"].toString();//获取天气
QString code =json["results"].toArray()[0].toObject()["now"].toObject()["code"].toString();//获取现在天气的图标信号
}
});