細 文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag… |
|||
| (同じ利用者による、間の6版が非表示) | |||
| 38行目: | 38行目: | ||
<br><br> | <br><br> | ||
== | == JSONファイルの読み込み == | ||
==== 単一オブジェクトの場合 ==== | |||
<syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
# Sample.json | # Sample.json | ||
| 107行目: | 108行目: | ||
for (const auto impvalue : impvalues) { // 範囲ループforで取得する場合 | for (const auto impvalue : impvalues) { // 範囲ループforで取得する場合 | ||
vecvalues.push_back(impvalue.toString()); | vecvalues.push_back(impvalue.toString()); | ||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== 配列オブジェクトの場合 ==== | |||
<syntaxhighlight lang="json"> | |||
# Sample.json | |||
{ | |||
{ | |||
"description": "SomeDescription", | |||
"message": "SomeMessage" | |||
}, | |||
{ | |||
"description": "Home", | |||
"message": "Welcome", | |||
"imp":["awesome","best","good"] | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
#include <QFile> | |||
#include <QJsonDocument> | |||
#include <QJsonArray> | |||
#include <QJsonObject> | |||
#include <QString> | |||
#include <QDebug> | |||
void readJson() | |||
{ | |||
// Jsonファイルの読み込み | |||
QFile file("Sample.json"); | |||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | |||
qDebug() << "JSONファイルの読み込みに失敗しました" << filePath; | |||
return; | |||
} | |||
QByteArray jsonData = file.readAll(); | |||
// ファイルを閉じる | |||
file.close(); | |||
QJsonDocument document = QJsonDocument::fromJson(jsonData); | |||
if (!document.isArray()) { | |||
qDebug() << "JSONファイルに配列オブジェクトがありません"; | |||
return; | |||
} | |||
QJsonArray jsonArray = document.array(); | |||
foreach (const QJsonValue &value, jsonArray) { | |||
QJsonObject obj = value.toObject(); | |||
QString desc = obj["description"].toString(); | |||
QString message = obj["message"].toString(); | |||
// キーの値が配列の場合、配列を文字列に変換する | |||
QJsonArray impvalues = obj["imp"].toArray(); | |||
// 配列から要素を指定して取得する場合 | |||
QString impvalue1 = impvalues[1].toString(); | |||
// ...略 | |||
} | } | ||
} | } | ||
| 194行目: | 257行目: | ||
<br><br> | <br><br> | ||
== | == JSONファイルの更新 == | ||
以下に示すようなJSONファイルにおいて、<u>appDesc</u>オブジェクトにある<u>description</u>キーの値を変更する。<br> | 以下に示すようなJSONファイルにおいて、<u>appDesc</u>オブジェクトにある<u>description</u>キーの値を変更する。<br> | ||
<syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
| 290行目: | 353行目: | ||
<br><br> | <br><br> | ||
== JSONファイルのオブジェクトの削除 == | |||
以下に示すようなJSONファイルにおいて、各オブジェクトから<u>fuga</u>キーの値がffのオブジェクトを削除する。<br> | |||
<syntaxhighlight lang="json"> | |||
[ | |||
{ | |||
"hoge": "aa", | |||
"piyo": "bb", | |||
"fuga": "cc" | |||
}, | |||
{ | |||
{ | |||
"hoge": "dd", | |||
"piyo": "ee", | |||
"fuga": "ff" | |||
}, | |||
{ | |||
"hoge": "gg", | |||
"piyo": "hh", | |||
"fuga": "ii" | |||
} | |||
] | |||
</syntaxhighlight> | |||
<br> | |||
JSONファイルの任意のオブジェクトを削除する場合、以下に示す手順に従う。<br> | |||
# JSONファイルの読み込み | |||
#: <code>QFile</code>クラスおよび<code>QJsonDocument</code>クラスを使用して、JSONファイルを読み込む。 | |||
# "fuga"キーの値の確認 | |||
#: <code>QJsonDocument</code>クラスを使用して読み込んだデータを解析する。 | |||
#: 各オブジェクトの"fuga"キーの値を確認して、値が"ff"と一致するものを特定する。 | |||
# 条件に一致するオブジェクトの削除 | |||
#: 条件に一致するオブジェクトを配列から削除する。 | |||
# 変更をファイルに保存 | |||
#: 変更後のJSONデータをファイルに再保存する。 | |||
<br> | |||
以下の例では、JSONファイルからデータを読み込み、各オブジェクトにあるfugaキーの値がffのオブジェクトを削除して、その結果を元のファイルに上書き保存している。<br> | |||
<syntaxhighlight lang="c++"> | |||
#include <QCoreApplication> | |||
#include <QFile> | |||
#include <QJsonDocument> | |||
#include <QJsonArray> | |||
#include <QJsonObject> | |||
#include <QDebug> | |||
int main(int argc, char *argv[]) | |||
{ | |||
QCoreApplication a(argc, argv); | |||
QFile file("/path/to/your/jsonfile.json"); | |||
if (!file.open(QIODevice::ReadOnly)) { | |||
qWarning("ファイルを開けませんでした。"); | |||
return -1; | |||
} | |||
QByteArray data = file.readAll(); | |||
QJsonDocument doc(QJsonDocument::fromJson(data)); | |||
QJsonArray array = doc.array(); | |||
// "fuga"キーの値が"ff"のオブジェクトを削除する | |||
QJsonArray newArray; | |||
QString targetfugaStr = "ff"; | |||
for (int i = 0; i < array.size(); ++i) { | |||
QJsonObject obj = array[i].toObject(); | |||
QString fugaString = obj["fuga"].toString() | |||
if (targetfugaStr != targetDate) { | |||
newArray.append(obj); | |||
} | |||
} | |||
// 変更をファイルに保存 | |||
QJsonDocument saveDoc(newArray); | |||
if (!file.open(QIODevice::WriteOnly)) { | |||
qWarning("ファイルを書き込みモードで開けませんでした"); | |||
return -1; | |||
} | |||
try { | |||
file.write(saveDoc.toJson()); | |||
} | |||
catch (QException &ex) { | |||
qWarning("JSONファイルの保存に失敗しました"); | |||
file.close(); | |||
return -1; | |||
} | |||
file.close(); | |||
return a.exec(); | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] | ||