使用QJson解析Qt通过UDP发送的JSON数据

时间:2022-12-15 15:23:58

Qt代码如下:

首先在pro文件中加入

QT += network

h文件内容:

使用QJson解析Qt通过UDP发送的JSON数据
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QtNetwork>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

     QUdpSocket *sender;
    QByteArray byteArray;
private slots:
     void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
使用QJson解析Qt通过UDP发送的JSON数据

cpp文件内容:

使用QJson解析Qt通过UDP发送的JSON数据
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QJsonObject>
#include<QJsonDocument>
#include<QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    sender = new QUdpSocket(this);

    QJsonObject rectJson;
    rectJson.insert("Type","Rectangle");
    rectJson.insert("height",42);
    rectJson.insert("widght",23);

    QJsonDocument rectJsonDoc;
    rectJsonDoc.setObject(rectJson);
     byteArray = rectJsonDoc.toJson(QJsonDocument::Compact);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

     QHostAddress address;
     address.setAddress(QString("192.168.230.140"));
       sender->writeDatagram(byteArray.data(),byteArray.size(),
                            address,4444);
}
使用QJson解析Qt通过UDP发送的JSON数据

Qt程序通过端口4444,向IP为192.168.230.140的主机发送JSON数据

4,C程序如下:

使用QJson解析Qt通过UDP发送的JSON数据
{
    int sock_fd;
    char rcv_buff[512];
    struct sockaddr_in client_addr;
    struct sockaddr_in server_addr;
    int client_len;
    int rcv_num = -1;

    if ((sock_fd = socket(AF_INET, SOCK_DGRAM,0)) < 0)
    {
        perror("socket create error\n");
        exit(1);
    }

    memset(&server_addr,0,sizeof(struct sockaddr_in));
    
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(4444);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    client_len = sizeof(struct sockaddr_in);

    if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in)) < 0)
    {
        perror("bind socket error.\n");
        exit(1);
    }
    
    while (1)
    {
        /*zero the buff of rvbsm and hvbsm? zhp*/
            
        rcv_num= recvfrom(sock_fd, rcv_buff, sizeof(rcv_buff), 0, (struct sockaddr*)&client_addr, &client_len);
        if (rcv_num>0)
        {
                    rcv_buff[rcv_num] = '\0';
                    printf("rx bsm num = %d\n",rcv_num);
                    //printf();
                        printf("%s %u says: %s\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port),rcv_buff);
                      //parse_UDP_data(rcv_num,rcv_buff);

                    if(rcv_buff != NULL)
                        {
                            cJSON* pJson = cJSON_Parse(rcv_buff);
                            if(pJson == NULL)
                                {
                                    printf("Json Parse failed.\n");
                                    return 0;
                                }
                            cJSON* pSub1 = cJSON_GetObjectItem(pJson,"Type");
                            cJSON* pSub2 = cJSON_GetObjectItem(pJson,"height");
                            cJSON* pSub3 = cJSON_GetObjectItem(pJson,"widght");
                            if(pSub1!=NULL)
                                {
                                    printf("Type : %s\n", pSub1->valuestring);
                                }
                            if(pSub2!=NULL)
                                {
                                    printf("height : %d\n", pSub2->valueint);
                                }
                            if(pSub3!=NULL)
                                {
                                    printf("widght : %d\n", pSub3->valueint);
                                }
                        }
                    
        }
        else
        {
            perror("recv BSM error\n");
            break;
        }
    }
    close(sock_fd);
   

}

输出结果为:

使用QJson解析Qt通过UDP发送的JSON数据