本文主要是介绍QT : Bson\Json互转,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简介
Bson 介绍
(1)官网 BSON (Binary JSON) Serialization
(2)百度百科 BSON_百度百科
Json介绍
菜鸟教程 JSON 教程 | 菜鸟教程
转换
QT /CPP
#include <QCoreApplication>
#include <QJsonDocument>
#include <QFile>
#include <QDebug>#include "bson/Value.h"BSON::Value gbsonDoc = BSON::Object{{"undefined", BSON::Value{}},{"int32", (BSON::int32)1},{"int64", (BSON::int64)1},{"double", 3.14},{"true", true},{"false", false},{"string", "foobar"},{"datetime", std::chrono::milliseconds{123}},{"object", BSON::Object{{"foo", "bar"}}},{"array", BSON::Array{1, 2, 3}}};int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);std::string bsonString = gbsonDoc.toBSON(); // bson结构->bson字符串BSON::Value bsonDoc = BSON::Value::fromBSON(bsonString); // bson字符串->bson结构QString jsonString = QString::fromLocal8Bit(gbsonDoc.toJSON().c_str()); // bson结构->json字符串QJsonDocument jsonDoc = QJsonDocument::fromJson(QByteArray::fromStdString(bsonDoc.toJSON())); // bson结构->QJsonDocumentBSON::Value bsonDocfromJson = BSON::Value::fromJSON(std::string(const_cast<const char *>(jsonString.toLocal8Bit().constData()))); // json字符串->bson结构qDebug() << "Bson String: " << QByteArray::fromStdString(bsonDoc.toJSON());qDebug() << "Json String: " << jsonString;qDebug() << "Json Document: " << jsonDoc;return a.exec();
}
参考:
https://github.com/trusch/libbson
资源:
QT 例子代码 - https://download.csdn.net/download/halo_hsuh/12284550
这篇关于QT : Bson\Json互转的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!