Qt Socket Server 收发JSON

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

之前文章写过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语言写的

4. 一点感想

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

 

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

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

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值