本文介绍了如何在Qt5中创建/读/写JSon文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Qt5有一个新的JSon解析器,我想使用它。问题是,它不是太清楚什么功能做外行人的术语,如何写它的代码。
我想知道在Qt5中创建Json文件的代码,以及封装是什么意思。
解决方案
示例从文件读取json
code> / * test.json * /
{
appDesc:{
description:SomeDescription,
message:SomeMessage
},
appName:{
description:Home,
message:Welcome,
imp:[awesome ,best,good]
}
}
void readJson()
{
QString val;
QFile文件;
file.setFileName(test.json);
file.open(QIODevice :: ReadOnly | QIODevice :: Text);
val = file.readAll();
file.close();
qWarning()<< val;
QJsonDocument d = QJsonDocument :: fromJson(val.toUtf8());
QJsonObject sett2 = d.object();
QJsonValue value = sett2.value(QString(appName));
qWarning()<<值;
QJsonObject item = value.toObject();
qWarning()<< tr(QJsonObject of description:)<<项目;
/ *包含字符串值获取值并转换为字符串* /
qWarning()< tr(QJsonObject [appName] of description:)<<商品描述];
QJsonValue subobj = item [description];
qWarning()<< subobj.toString();
/ * incase数组get数组并转换成字符串* /
qWarning()<< tr(QJsonObject [appName] of value:)< item [imp];
QJsonArray test = item [imp]。toArray();
qWarning()<< test [1] .toString();
}
OUTPUT
QJsonValue(object,QJsonObject({description:Home,imp:[awesome ,best,good],message:YouTube}))
pre>
QJsonObject of description:QJsonObject({description:Home,imp:[awesome, best,good],message:YouTube})
QJsonObject [appName] of description:QJsonValue(string,Home)
Home
QJsonObject [appName] value:QJsonValue(array,QJsonArray([awesome,best,good]))
best
示例从字符串读取json。 **
** 将json分配给下面的字符串,并使用上面的readJson()函数val =
'{
appDesc:{
description:SomeDescription,
message:SomeMessage
},
appName:{
description:Home,
message:Welcome,
imp:[awesome,best ,good]
}
}';
OUTPUT p>
QJsonValue(object,QJsonObject({description:Home,imp:[awesome,best good],message:YouTube}))
QJsonObject of description:QJsonObject({description:Home,imp:[awesome,best好的],消息:YouTube})
QJsonObject [appName]的描述:QJsonValue(string,Home)
Home
的值:QJsonValue(array,QJsonArray([awesome,best,good]))
best
Qt5 has a new JSon parser and I want to use it. The problem is that it isn't too clear about what the functions do in layman's terms and how to write code with it. That or I could be reading it wrong.
I want to know the code on creating a Json file in Qt5 and what "encapsulates" mean.
解决方案Example read json from file
/* test.json */ { "appDesc": { "description": "SomeDescription", "message": "SomeMessage" }, "appName": { "description": "Home", "message": "Welcome", "imp":["awesome","best","good"] } } void readJson() { QString val; QFile file; file.setFileName("test.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); val = file.readAll(); file.close(); qWarning() << val; QJsonDocument d = QJsonDocument::fromJson(val.toUtf8()); QJsonObject sett2 = d.object(); QJsonValue value = sett2.value(QString("appName")); qWarning() << value; QJsonObject item = value.toObject(); qWarning() << tr("QJsonObject of description: ") << item; /* incase of string value get value and convert into string*/ qWarning() << tr("QJsonObject[appName] of description: ") << item["description"]; QJsonValue subobj = item["description"]; qWarning() << subobj.toString(); /* incase of array get array and convert into string*/ qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"]; QJsonArray test = item["imp"].toArray(); qWarning() << test[1].toString(); }
OUTPUT
QJsonValue(object, QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) ) "QJsonObject of description: " QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) "QJsonObject[appName] of description: " QJsonValue(string, "Home") "Home" "QJsonObject[appName] of value: " QJsonValue(array, QJsonArray(["awesome","best","good"]) ) "best"
Example read json from string. ****assign json to string as below and use above readJson() function
val = ' { "appDesc": { "description": "SomeDescription", "message": "SomeMessage" }, "appName": { "description": "Home", "message": "Welcome", "imp":["awesome","best","good"] } }';
OUTPUT
QJsonValue(object, QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) ) "QJsonObject of description: " QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) "QJsonObject[appName] of description: " QJsonValue(string, "Home") "Home" "QJsonObject[appName] of value: " QJsonValue(array, QJsonArray(["awesome","best","good"]) ) "best"
这篇关于如何在Qt5中创建/读/写JSon文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!