Qt实现对json文件的解析

       json是一种轻量级的数据结构,其内部的结构是一种键值对(key-value)的组合,最外层是{ }。key是带双引号的字符串常亮,用于获取和存储;value的值可以是bool变量,字符串常量,对象或数组(也是一个key-value的组合,以 [ 开头,以 ] 结尾),具体json格式介绍可参考博客:https://www.cnblogs.com/hwtblog/p/8483573.html

     Qt中提供QJsonDocument,QJsonValue,QJsonObject,QjsonArray等类来实现对json文件的解析。以下面的json为例子,实现对json文件中数据的解析。

{“tunning params array” : [{"anc auto tuning filter count: 0,"anc mode" : "FFMode","anc params array" : [{"bypass" : true,"frequency": 5000,},{"bypass" : false,"frequency": 1000,}]},{"anc auto tuning filter count: 1,"anc mode" : "FBMode","anc params array" : [{"bypass" : true,"frequency": 5000,},{"bypass" : false,"frequency": 1000,}]}]
}

    实现对json文件数据的读取,具体实现的API如下所示:

#include <QList>
#include <QString>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonValue>struct AncParameter {bool bypass;int frequency;
};struct TuningParameter {int ancAutoTuningFilterCount;QList<AncParameter> ancParaArray;QString ancMode;
};QList<AncParameter> readParamsFromJsonFile(QString jsonFile)
{QList<AncParameter> result;//打开json文件,读取所有数据QFile file(jsonFile);if (!file.open(QIODevice::ReadOnly)) return result;QByteArray allData = file.readAll();file.close();//读取具体的json数据QJsonParseError jsonError;QJsonDocument jsonDoc = QJsonDocument::fromJson(allData, &jsonError);if (jsonError != QJsonParseError.NoError) return result;if (!jsonDoc.isObject()) return result;QJsonObject rootObject = jsonDoc.object();if (rootObject.contains("tunning params array")) {QJsonValue ancTuningParaArray = rootObject.value("tunning params array");if (ancTuningParaArray.isArray()) {QJsonArray tuningParameterArray = ancTuningParaArray.toArray();for (int i = 0; i < tuningParameterArray.count(); i++) {QJsonValue tuningPara = tuningParameterArray[i];if (tuningPara.isObject()) {TuningParameter tuningParameter;QJsonObject tuningParameterObject = tuningPara.toObject();if (tuningParameterObject.contains("anc auto tuning filter count")) {QJsonValue ancAutoTuningFilterCount = tuningParameterObject.value("anc auto tuning filter count");if (ancAutoTuningFilterCount.isDouble())tuningParameter.ancAutoTuningFilterCount = (int)ancAutoTuningFilterCount.toDouble();} else if (tuningParameterObject.contains("anc mode")) {QJsonValue ancMode = ancParameterObject.value("anc mode");if (ancMode.isString()) {QString ancModeString = ancMode.toString();if (QString::compare("FFMode", ancModeString, Qt::CaseInsensitive))tuningParameter.ancMode = "FFMode";elsetuningParameter.ancMode = "FBMode";}} else if (tuningParameterObject.contains("anc params array")) {QJsonValue ancParameterArray = tuningParameterObject.value("anc params array");if (ancParameterArray.isArray()) {QJsonArray ancArray = ancParameterArray.toArray();for (int i = 0; i < ancArray.count(); i++) {AncParameter ancParameter;QJsonValue ancParaValue = ancArray[i];if (ancParaValue.contains("bypass")) {QJsonValue bypassValue = ancParaValue.value("bypass");if (bypassValue.isBool())ancParameter.bypass = bypassValue.toBool();} else if (ancParaValue.contains("frequency")) {QJsonValue frequencyValue = ancParaValue.value("frequency");if (frequencyValue.isDouble())ancParameter.bypass = (int)frequencyValue.toDouble();}tuningParameter.ancParaArray.append(ancParameter);}}}result.append(tuningParameter);}}}}return result;
}

   至于对json文件的存取,就是将list中的输入数据写入到 json文件中,原理类似。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/382244.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

解决QTreeWidget中item无法整行同时显示相同颜色

对于QTreeWidget来说&#xff0c;继承自QTreeView&#xff0c;因此设计QTreeWidget中的样式设计&#xff0c;归根结底是对QTreeView的样式设计&#xff0c;而对于QTreeView中item样式设计&#xff0c;可以分为对branch和item设计&#xff0c;另外QT Style Sheet可以通过设置sho…

279. 完全平方数 golang

Me func checkRecord(s string) bool {return !(strings.Count(s, "A") > 1 || strings.Contains(s, "LLL")) }golang的string用法 func EqualFold func EqualFold(s, t string) bool 判断两个utf-8编码字符串&#xff08;将unicode大写、小写、标题…

Qt自定义对话框中边框阴影实现

1. 对于Window系统或者Unix系统&#xff0c;QDialog有一个默认的边框(样式看起来有点复古)&#xff0c;不过Qt可以提供自定义的边框设计&#xff0c;通过设置对话框相关属性&#xff1a; setWindowFlags(Qt::Dialog | Qt:: FramelessWindowHint); //设置不适应默认边框 setAt…

344. 反转字符串 golang

Me 双指针操作。首指针和尾指针 func reverseString(s []byte) {left,right:0,len(s)-1for left < right && len(s) ! 0 {s[left],s[right]s[right],s[left]leftright--} }

Qt自定义数据类型注册meta-object system

在Qt项目开发过程中&#xff0c;有时候会碰到需要使用自定义数据类型作为信号或者槽的参数进行数据传输&#xff0c;由于信号或者槽中的参数必须为Qt meta-object 系统中能够识别的参数&#xff0c;但是自定义数据类型仅仅只是程序代码编写者自己知道&#xff0c;编译器在编译的…

Qt使用invokeMethod反射机制实现线程间的通信

对于Qt来说&#xff0c;UI线程是主线程&#xff0c;对于同一UI线程中对象的通信可以通过connect进行信号与槽关联来实现&#xff0c;但是当UI中对象A中的子线程B需要和另外UI对象C进行通信的时候&#xff0c;如果这个时候使用connect来进行通信的话,需要B对象和A对象进行关联将…

Qt eventFilter实现信号与槽

对于Qt来说&#xff0c;信号与槽机制是其通信的一大亮点&#xff0c;但是Qt中也提供了其他的方法实现数据间的通信&#xff0c;如invokeMethod反射机制实现通信(https://blog.csdn.net/xx18030637774/article/details/105017306),另外Qt还提供了使用eventFilter来实现数据通信&…

318. 最大单词长度乘积 golang

question 给定一个字符串数组 words&#xff0c;找到 length(word[i]) * length(word[j]) 的最大值&#xff0c;并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词&#xff0c;返回 0。 示例 1: 输入: [“abcw”,“baz”,“foo”,“…

Qt编译错误:无法解析的外部符号 __imp__CloseServiceHandle __imp__OpenSCManager

在项目开发的过程&#xff0c;我在Qt的pro工程文件中添加了一个静态库&#xff0c;编译工程后报了这个错误&#xff0c;一开始以为是静态库有问题&#xff0c;验证之后发现静态库是正确的&#xff0c;最后才发现CloseServiceHandle和openSCManager这几个API都保存在系统库Advap…

242. 有效的字母异位词 golang

242. 有效的字母异位词 两个单词如果包含相同的字母&#xff0c;次序不同&#xff0c;则称为字母易位词(anagram) 思路 字符转换成bytebyte排序byte挨个对比 // 两个单词如果包含相同的字母&#xff0c;次序不同 func quickSort(arr []byte, left int, right int) {if left &…

leetcode验证冒泡排序效率

结论&#xff1a;快排比冒泡慢 bubbleSort leetcode_bubbleSort // 两个单词如果包含相同的字母&#xff0c;次序不同 func bubbleAsort(values []byte) {for i : 0; i < len(values)-1; i {for j : i1; j < len(values); j {if values[i]>values[j]{values[i],va…

QString转化为const char *出现乱码问题

对于Qt项目开发中&#xff0c;常常会遇到QString和const char*的转化问题&#xff0c;Qt也提供了响应的API接口&#xff0c;可以把QString转化为QByteArray&#xff0c;然后再转化为const char *,具体代码如下&#xff1a; QString string("helloWorld"); const cha…

go有没有引用类型

没有引用类型 go没有引用类型在函数内部修改变量&#xff1f; 指针类型 测试代码如下 package mainimport "fmt"func add(a int) {fmt.Println("a int")fmt.Printf("%d\n", &a)a 1 }func add2(a *int) {fmt.Println("a *int"…

Qt中的QByteArray和自定义结构体之间的相互转换

在Qt项目开发中&#xff0c;经常会碰到自定义结构体和字符数组之间的转换问题&#xff0c;不妨假设结构体名字为custom_struct, 字符数组名字为array_data 1. QByteArray转换为自定义结构体 custom_struct *struct_data reinterpret_cast<custom_struct *>(array_data…

20. 有效的括号 golang(2)

ASCII码来处理 第一种方法使用了栈的包&#xff0c;这种方式使用了ASCII码处理&#xff0c;但是&#xff0c;做题的时候还是需要查 //ASCII码 {123 }125 (40 )41 [91 ]93 func isValid(s string) bool {size : len(s)stack : make([]byte, size)top : 0 for i:0; i<size; i …

Qt控件大小自适应电脑分辨率问题

在最近的Qt工具开发工程中&#xff0c;发现一个问题&#xff1a;在自己电脑(分辨率是1366*768)上开发出来的工具&#xff0c;发布给同事(分辨率1920*1280)使用的过程中却出现了空间字体&#xff0c;边框等变形的问题&#xff0c;最后发现原因是因为在样式设计中&#xff0c;混合…

27. 移除元素 golang

空间复杂度O(1) 题目中不需要保持数组按照原来的顺序。 返回值是删除完key的剩余元素个数。所以我们使用前后指针&#xff0c;f指针指到key的位置&#xff0c;l指最后非key的位置。交换元素。 func removeElement(nums []int, val int) int {i, j : 0, len(nums) - 1for {for …

Qt 编译错误 LINK2001:无法解析的外部符号 public: virtual struct QMetaObject const thiscall Widget::metaObject

对于Qt来说&#xff0c;signal和slots是Qt的核心&#xff0c;而对于信号来说&#xff0c;只有继承了QObject的类&#xff0c;并且在类中添加Q_OBJECT宏&#xff0c;信号才能有效地和槽建立连接。可是在Qt开发的前期&#xff0c;可能预测不到需要在类中添加信号&#xff08;也就…

122. 买卖股票的最佳时机 II golang

Me 输入: [7,1,5,3,6,4] 输出: 7 输入: [1,2,3,4,5] 输出: 4 输入: [7,6,4,3,1] 输出: 0 得出的结论就是当有了股票只要明天跌就今天卖。只要明天涨今天就买&#xff08;只限于做题&#xff1f;&#xff1f;&#xff1f;&#xff09; func maxProfit(prices []int) int {res…

Qt中QLabel的背景图片设置问题

在Qt开发的过程中&#xff0c;经常会遇到设置Label的背景图片问题&#xff0c;通常可以有一下两种方法设置&#xff1a; 方法1&#xff1a;通过setScaleContents来设置(有时候会出现svg图片无法自适应label大小的问题) QPixmap pixmap("***.svg"); label->setFix…