30

Qt Socket Server 收发JSON

 5 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzUxMTk4MzY3MA%3D%3D&%3Bmid=2247484547&%3Bidx=1&%3Bsn=422efcf214a6410202486466a0e81912
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,麻烦点点击下文末的广告,感谢~

之前文章写过Linux C Socket 收发 Json 数据,最近用 Qt Server 实现了一遍。给我自己的感觉就是 cJSON 接口与 Qt 封装的一些接口是共通的: Qt 封装了 QJsonObject 来对 Json 对象操作,如增删改查;封装了 QJsonDocument 来进行一些序列化与反序列化的操作 ( 可能不准确 )

重要消息:虽然明天是端午节,但是还是要去公司学习。。。

后续可能会在公众号中写一点儿理财相关的知识,待定中......

程序中用到了Qt 的 Socket Server ,但主要介绍下 Qt 中如何操作 Json 数据,将接收到的字节流转换为 Json 对象,又如何将 Json 对象转换为字节流。

一、  程序介绍

1. Json 操作相关函数

为了与C语言写的对比,同样写了 3 个函数:

Qt :

int ParseRecvJsonData(const QByteArray &recvdata, int *outLogLevel);

int WriteLogLevelToFileJson(const QString &filePathName, const int logLevel);

int CreateRespondInfoJson(QByteArray *respondInfoJson,

const QByteArray &recvJsonData,

const int writeFileRet);

C:

int ParseRecvJsonData(const char * recvdata, int *outLogLevel);

int WriteLogLevelToFileJson(const char *filePathName, int logLevel);

int CreateRespondInfoJson(char *respondInfoJson,

const char* recvJsonData,

const int writeFileRet);

解析Json字节流为 Json 对象,提取所需信息

将所需信息组装成Json对象写入配置文件

将字节流转换为Json对象并添加数据,组装成响应信息

2. Qt 中对 Json 操作的具体实现

直接加注释进行说明

int TcpServerRecvImage::CreateRespondInfoJson(QByteArray *respondInfoJson,

const QByteArray &recvJsonData, const int writeFileRet)

{

//使用QJsonDocument判断字节流能否转成Json对象

QJsonParseError jsonError;

QJsonDocument jsonRecvData(QJsonDocument::fromJson(recvJsonData, &jsonError));


if(jsonError.error != QJsonParseError::NoError)

{

qDebug() << "parse json error!";

return -1;

}

//通过QsonDocument将字节流转为Json对象

QJsonObject rootObject = jsonRecvData.object();

//向Json对象中追加数据

rootObject.insert("Result","FAIL");


if(0 == writeFileRet)

{

//修改对应数据;可以思考下是如何实现修改前与修改后数据所占空间不同

rootObject["Result"] = "SUCCESS";

}

//将Json对象转换为字节流

QJsonDocument documentJson;

documentJson.setObject(rootObject);

QByteArray bytearrayJson = documentJson.toJson();


respondInfoJson->clear();

respondInfoJson->append(bytearrayJson);


return 0;

}


int TcpServerRecvImage::WriteLogLevelToFileJson(const QString &filePathName,

const int logLevel)

{

//Qt文件操作

QFile f(filePathName);

if(!f.open(QIODevice::WriteOnly | QIODevice::Text))

{

qDebug() << "Open failed.";

return -1;

}


// create JSON Object

QJsonObject logLevelJson;

logLevelJson.insert("logLevel",QString::number(logLevel));


QJsonDocument documentJson;

documentJson.setObject(logLevelJson);

QByteArray bytearrayJson = documentJson.toJson();

//使用QTextStream,简化文件操作

QTextStream txtWrite(&f);

txtWrite << bytearrayJson;


f.close();


return 0;

}

3. 程序效果

客户端程序是之前的C语言写的

QNVZ3aV.png!web

qmU32qY.png!web

4.  一点感想

最近一段时间的状态就是不断看书,写Demo程序,并将以前看的一些知识结合起来,不断提炼总结,有一种这就是我想要的生活的错觉;

数据结构如何应用在程序中呢?

应用程序如何变得更强壮呢?

如需完整工程可在公众号后台留言:

MZzeIjR.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK