How to serialize to JSON in Qt

2019-03-18 07:15发布

How can I JSON serialize a QVariant (or other type of data) in Qt. I don't want to use an external third party library like QJson

4条回答
Fickle 薄情
2楼-- · 2019-03-18 07:21

See this JSON Save Game example on serialization of an object to a JSON document.

查看更多
在下西门庆
3楼-- · 2019-03-18 07:27

There are no internal tools in Qt to do it. You will have to use a lib. Or roll your own implementation

查看更多
何必那么认真
4楼-- · 2019-03-18 07:33

Just to mention, as of Qt5, JSON is officially supported:

JSON Support in Qt

QVariant id(1), name("John Doe");
QJsonObject json;

json["Name"] = name.toString();
json.insert("id", id.toInt());
查看更多
别忘想泡老子
5楼-- · 2019-03-18 07:43

Parsing JSON with QT using standard QT library.

BTW: why don't you want to use QJson? It nicely encapsulates all the QScriptValueIterator stuff, making your code easier to read.

查看更多
登录 后发表回答