Qt Json解析一连串数据点Points

软件环境:QT Version 5.14.0

1.目标

问题背景:鸿蒙OS组件开发需求:解析JS层后并绘制一段折现;基于此背景利用qt框架下的JSON解析模块实现此需求。此案例仅仅是对qt框架的一个json模块探索,与实际工作内容无关。
1.利用qt json模块解析json文件,实现对一组数据点point的解析
2.对解析出的点并绘制一段折线,本文不涉及此内容,感兴趣可以自己补充实现

2.JSON文件

{
“points”: [
[ 10, 20 ],
[ 10, 30 ],
[ 10, 50 ]
]
}
在这里插入图片描述

3.代码结构

在这里插入图片描述

4.代码示例

Point.h

#ifndef POINT_H
#define POINT_H
#include<iostream>
#include<utility>
#include<QVector>
#include<QPair>
#include<QPoint>
using Points = QVector<QPoint>;
#endif // POINT_H

Polyline.h

#ifndef SHAPE_POINT_H
#define SHAPE_POINT_H
#include"Point.h"


class Shape_Point
{
public:
    Shape_Point();
    void SetPoints(Points& points);
    Points& GetPoints();
    void PaintPoint();

private:
    //point contain x value and y value;
    //Point point;
    QPoint point;
    //vector contain all parse points;
    Points points;

};
#endif // SHAPE_POINT_H

Polyline.cpp

#include "Polyline.h"
#include<QDebug>
Shape_Point::Shape_Point():point(0,0)
{

}
void Shape_Point::SetPoints(Points& points)
{
    this->points=points;
}
Points& Shape_Point::GetPoints()
{
    return this->points;
}
void Shape_Point::PaintPoint()
{
    int size=points.size();
    for(int i=0;i<size;i++){
        qDebug()<<points[i].x()<<" "<<points[i].y();
    }
}

main.cpp

#include <QCoreApplication>
#include <QJsonObject>
#include <QJsonObject>
#include<QJsonArray>
#include<Polyline.h>
#include<QFile>
#include<QDebug>
#include<QTextStream>
#include<QJsonDocument>
/*
 {
  "points":[[10,20],[10,30]]
 }
 */
/*in order to parse points to vector*/
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QFile file("F:\\QT Project\\Json_1\\json.json");
    if(!file.open(QIODevice::ReadOnly)){
        qDebug()<<"open error";
    }

    QByteArray byteArr=file.readAll();
    QString jsonStr=QString(byteArr);
    qDebug()<<jsonStr;
    
    //parse json points
    QJsonParseError err;
    QJsonDocument doc=QJsonDocument::fromJson(byteArr,&err);
    QJsonObject rootobj=doc.object();

    Shape_Point polyline_point;
    Points points_;
    QPoint point;

    QJsonValue pointsValue=rootobj.value("points");

    if(pointsValue.type()==QJsonValue::Array){
        QJsonArray pointsArr=pointsValue.toArray();
        for(int i=0;i<pointsArr.count();i++){
            QJsonValue pointsValue=pointsArr.at(i);
            if(pointsValue.type()==QJsonValue::Array ){
                    QJsonArray pointsArr=pointsValue.toArray();
                    if(pointsArr.size()>=2){
                        point.setX(pointsArr.at(0).toInt());
                        point.setY(pointsArr.at(1).toInt());
                        points_.push_back(point);
                    }
                 }
            }
        polyline_point.SetPoints(points_);
     }

    //print polyline's all points;
    polyline_point.PaintPoint();

    return a.exec();
    }

5.运行效果

在这里插入图片描述

6.思考

如果QT框架中没有实现点的定义(Qpoint),又该如何实现此需求?
参考我的另一篇文章:
链接: link.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt中,可以使用QJsonDocument和QJsonObject类来解析和处理JSON数据。 首先,需要将JSON数据转换为QJsonDocument对象。可以使用QJsonDocument::fromJson()方法将JSON字符串或字节数组转换为QJsonDocument对象。例如: ```cpp QString jsonString = "{\"name\":\"John\",\"age\":30}"; QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8()); // 或者 QByteArray jsonData = "{\"name\":\"John\",\"age\":30}"; QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData); ``` 然后,可以通过调用`jsonDoc.object()`方法获取QJsonObject对象,以便访问和处理JSON数据的键值对。例如: ```cpp QJsonObject jsonObj = jsonDoc.object(); QString name = jsonObj["name"].toString(); int age = jsonObj["age"].toInt(); ``` 对于嵌套的JSON结构,可以通过递归方式进行解析。例如,如果JSON数据包含一个嵌套的数组: ```cpp QString jsonArrayString = "{\"people\":[{\"name\":\"John\",\"age\":30},{\"name\":\"Alice\",\"age\":25}]}"; QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonArrayString.toUtf8()); QJsonObject jsonObj = jsonDoc.object(); QJsonArray peopleArray = jsonObj["people"].toArray(); foreach (const QJsonValue& value, peopleArray) { QJsonObject personObj = value.toObject(); QString name = personObj["name"].toString(); int age = personObj["age"].toInt(); // 处理每个人的数据 } ``` 以上是一个简单的JSON解析示例,根据实际情况,可能需要根据JSON数据的结构进行更复杂的处理和解析

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex1_Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值