@TOC
大部分参考博客
不过由于修改json的方式存在问题,我这边修改了一下,可以运行了。
可以用作配置相关运行参数了
使用方式
使用qtcreator新建工程,直接替换main.c
中代码即可编译运行。
效果
原来json内容如下
{
"class": [
{
"age": "18",
"home": "xx",
"name": "aron566"
},
{
"age": "19",
"home": "xx",
"name": "aron566"
}
]
}
修改后:
{
"class": [
{
"age": "45",
"home": "xx",
"name": "aron566"
},
{
"age": "19",
"home": "xx",
"name": "aron566"
}
]
}
测试源码
#include "mainwindow.h"
#include <QApplication>
int ReadJson(int argc, char *argv[]);
int WriteJson(int argc, char *argv[]);
int WriteJsonx(int argc, char *argv[]);
int ModifiedJson(int argc, char *argv[]);
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
WriteJsonx(argc, argv);
// ReadJson(argc, argv);
ModifiedJson(argc, argv);
return 0;
// MainWindow w;
// w.show();
// return a.exec();
}
/*
读取json
{
"name": "aron566",
"age": "18",
"home": "xx"
}
*/
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
/*
读取json
{
"name": "aron566",
"age": "18",
"home": "xx"
}
*/
/*读取json格式数据*/
int ReadJson(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
QFile file("test.json");
if(!file.open(QIODevice::ReadOnly)) {
qDebug() << "File open failed!";
} else {
qDebug() <<"File open successfully!";
}
QJsonDocument jdc(QJsonDocument::fromJson(file.readAll()));
QJsonObject obj = jdc.object();
qDebug() << obj.value("name").toString()
<< obj.value("age").toString()
<< obj.value("home").toString();
return 0;
}
/*写出json格式数据*/
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
int WriteJson(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
QFile file("test.json");
if(!file.open(QIODevice::WriteOnly)) {
qDebug() << "File open failed!";
} else {
qDebug() <<"File open successfully!";
}
QJsonObject obj;
obj["age"] = "18";
obj["name"] = "aron566";
obj.insert("home", "xx");
QJsonDocument jdoc(obj);
file.write(jdoc.toJson());
file.flush();
return 0;
}
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
/*使用QJsonArray*/
/*
{
"class": [
{
"age": "18",
"home": "xx",
"name": "aron566"
},
{
"age": "19",
"home": "xx",
"name": "aron566"
}
]
}
*/
int WriteJsonx(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
QFile file("test.json");
if(!file.open(QIODevice::WriteOnly)) {
qDebug() << "File open failed!";
} else {
qDebug() <<"File open successfully!";
}
QJsonObject rootObj;
QJsonObject obj;
obj["age"] = "18";
obj["name"] = "aron566";
obj.insert("home", "xx");
QJsonObject obj1;
obj1["age"] = "19";
obj1["name"] = "aron566";
obj1.insert("home", "xx");
QJsonArray classArray;
classArray.append(obj);
classArray.append(obj1);
rootObj["class"] = classArray;
QJsonDocument jdoc(rootObj);
file.write(jdoc.toJson());
file.flush();
return 0;
}
/*修改Json数据*/
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
/*先读,修改,再写*/
int ModifiedJson(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
QFile file("test.json");
if(!file.open(QIODevice::ReadWrite)) {
qDebug() << "File open failed!";
} else {
qDebug() <<"File open successfully!";
}
QJsonDocument jdc(QJsonDocument::fromJson(file.readAll()));
QJsonObject rootObj = jdc.object();
QJsonObject subObj;
QJsonArray subArray;
/*判断是否是数组*/
if(rootObj.contains("class") && rootObj["class"].isArray())
{
qDebug() << "check array is ok!";
subArray = rootObj.value("class").toArray();
qDebug() << "数组长度大小:" << subArray.size();
for(int i = 0; i< subArray.size(); i++)
{
/*转换为子对象*/
subObj = subArray.at(i).toObject();
if (subObj.contains("age") && subObj["age"].isString())
{
qDebug() << "age is:" << subObj["age"].toString();
}
if (subObj.contains("home") && subObj["home"].isString())
{
qDebug() << "home is:" << subObj["home"].toString();
}
if (subObj.contains("name") && subObj["name"].isString())
{
qDebug() << "name is:" << subObj["name"].toString();
}
if(subObj["age"].toString() == "18")
{
qDebug() << "写入 age 45";
/*两种方式更换age这个对应值*/
subObj["age"] = "25";/**< 第一种*/
subObj.remove("age");/**< 第二种先移除*/
subObj.insert("age" ,"45");/**< 第二种再插入*/
subArray.replace(i ,subObj);
rootObj.insert("class" ,subArray);
}
}
}
jdc.setObject(rootObj);
file.seek(0);/*从头开始写*/
file.write(jdc.toJson());
file.flush();
return 0;
}
/*
* JSON格式如下:
{
"first fruit":
{
"describe":"an apple",
"icon":"appleIcon",
"name":"apple"
},
"second fruit":
{
"describe":"an orange",
"icon":"orangeIcon",
"name":"orange"
},
"three fruit array":
[
"eat 0",
"eat 1",
"eat 2",
"eat 3",
"eat 4"
]
}
解析:
#include <QJsonDocument>
#include <QJsonParseError>
#include <QFile>
#include <QJsonObject>
#include <QDebug>
#include <QJsonArray>
void read()
{
QFile loadFile("D:\\1.json");
if(!loadFile.open(QIODevice::ReadOnly))
{
qDebug() << "could't open projects json";
return;
}
QByteArray allData = loadFile.readAll();
loadFile.close();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
if(json_error.error != QJsonParseError::NoError)
{
qDebug() << "json error!";
return;
}
QJsonObject rootObj = jsonDoc.object();
QStringList keys = rootObj.keys();
for(int i = 0; i < keys.size(); i++)
{
qDebug() << "key" << i << " is:" << keys.at(i);
}
//因为是预先定义好的JSON数据格式,所以这里可以这样读取
if(rootObj.contains("first fruit") && rootObj["first fruit"].isObject())
{
QJsonObject subObj = rootObj["first fruit"].toObject();
if (subObj.contains("describe") && subObj["describe"].isString())
{
qDebug() << "describe is:" << subObj["describe"].toString();
}
if (subObj.contains("icon") && subObj["icon"].isString())
{
qDebug() << "icon is:" << subObj["icon"].toString();
}
if (subObj.contains("name") && subObj["name"].isString())
{
qDebug() << "name is:" << subObj["name"].toString();
}
}
if(rootObj.contains("second fruit") && rootObj["second fruit"].isObject())
{
QJsonObject subObj = rootObj["second fruit"].toObject();
if (subObj.contains("describe") && subObj["describe"].isString())
{
qDebug() << "describe is:" << subObj["describe"].toString();
}
if (subObj.contains("icon") && subObj["icon"].isString())
{
qDebug() << "icon is:" << subObj["icon"].toString();
}
if (subObj.contains("name") && subObj["name"].isString())
{
qDebug() << "name is:" << subObj["name"].toString();
}
}
if(rootObj.contains("three fruit array") && rootObj["three fruit array"].isArray())
{
QJsonArray subArray = rootObj.value("three fruit array").toArray();
for(int i = 0; i< subArray.size(); i++)
{
if (subArray[i].isString())
{
qDebug() << i<<" value is:" << subArray.at(i).toString();
}
}
}
}
*/