Qt5 には新しい JSON パーサーがあり、それを使用したいと考えています。問題は、関数が素人の言葉で何をするのか、それを使ってコードを書く方法があまり明確でないことです。それまたは私はそれを間違って読んでいる可能性があります。
Qt5 で JSON ファイルを作成する際のコードと、「カプセル化」の意味を知りたいです。
例: ファイルから json を読み取る
/* 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;
/* in case of string value get value and convert into string*/
qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];
QJsonValue subobj = item["description"];
qWarning() << subobj.toString();
/* in case 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();
}
出力
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"
例: 文字列から json を読み取る
以下のように json を文字列に割り当て、readJson()
前に示した関数を使用します。
val =
' {
"appDesc": {
"description": "SomeDescription",
"message": "SomeMessage"
},
"appName": {
"description": "Home",
"message": "Welcome",
"imp":["awesome","best","good"]
}
}';
出力
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"
悲しいことに、多くの JSON C++ ライブラリには、使いやすい API が含まれていますが、JSON は簡単に使用できるように設計されています。
そこで、上記の回答の 1 つに示されている JSON ドキュメントでgSOAP ツールからjsoncppを試しました。これは、JSON 形式で std::cout に書き込まれる C++ で JSON オブジェクトを構築するために jsoncpp で生成されたコードです。
value x(ctx);
x["appDesc"]["description"] = "SomeDescription";
x["appDesc"]["message"] = "SomeMessage";
x["appName"]["description"] = "Home";
x["appName"]["message"] = "Welcome";
x["appName"]["imp"][0] = "awesome";
x["appName"]["imp"][1] = "best";
x["appName"]["imp"][2] = "good";
std::cout << x << std::endl;
これは、std::cin から JSON を解析し、その値を抽出するために jsoncpp によって生成されたコードです (必要に応じて置き換えUSE_VAL
ます)。
value x(ctx);
std::cin >> x;
if (x.soap->error)
exit(EXIT_FAILURE); // error parsing JSON
#define USE_VAL(path, val) std::cout << path << " = " << val << std::endl
if (x.has("appDesc"))
{
if (x["appDesc"].has("description"))
USE_VAL("$.appDesc.description", x["appDesc"]["description"]);
if (x["appDesc"].has("message"))
USE_VAL("$.appDesc.message", x["appDesc"]["message"]);
}
if (x.has("appName"))
{
if (x["appName"].has("description"))
USE_VAL("$.appName.description", x["appName"]["description"]);
if (x["appName"].has("message"))
USE_VAL("$.appName.message", x["appName"]["message"]);
if (x["appName"].has("imp"))
{
for (int i2 = 0; i2 < x["appName"]["imp"].size(); i2++)
USE_VAL("$.appName.imp[]", x["appName"]["imp"][i2]);
}
}
このコードは、gSOAP 2.8.28 の JSON C++ API を使用します。人々がライブラリーを変更するとは思いませんが、この比較は JSON C++ ライブラリーの全体像を把握するのに役立つと思います。
それを使用する方法の例は素晴らしいでしょう。Qt フォーラムにはいくつかの例がありますが、公式ドキュメントを拡張する必要があることは間違いありません。
QJsonDocument
それだけでは実際には何も生成されません。データを追加する必要があります。QJsonObject
これは、、QJsonArray
およびQJsonValue
クラスによって行われます。最上位の項目は、配列またはオブジェクトのいずれかである必要があります (1
有効な json ドキュメントではないためです{foo: 1}
。)