<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QJsonDocument::toJson with subobjects]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I'm trying to output json data by using QJsonDocument and QJsonObject, but I have a problem with subobjects. The output always contains <em>null</em> for subojects. Below a small example:</p>
<pre><code>QVariantHash sub;
sub.insert("id", 2);
sub.insert("name", "world");

QVariantHash root;
root.insert("id", 1);
root.insert("name", "hello");
root.insert("sub", QJsonObject::fromVariantHash(sub));

QString output = QJsonDocument(QJsonObject::fromVariantHash(root)).toJson(QJsonDocument::Compact);
</code></pre>
<p dir="auto"><strong>Output</strong> = {"id":1,"name":"hello","sub":null}</p>
<p dir="auto">What I am missing?<br />
Best regards,<br />
Jonathan</p>
]]></description><link>https://forum.qt.io/topic/63466/qjsondocument-tojson-with-subobjects</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 15:42:45 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/63466.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 28 Jan 2016 14:50:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QJsonDocument::toJson with subobjects on Thu, 28 Jan 2016 15:09:06 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">you try to add a QJsonObject to root.<br />
If you try to add the QVariantHash to root you will get a valid json</p>
<pre><code>QVariantHash sub;
sub.insert("id", 2);
sub.insert("name", "world");

QVariantHash root;
root.insert("id", 1);
root.insert("name", "hello");
root.insert("sub", sub);

QString output = QJsonDocument(QJsonObject::fromVariantHash(root)).toJson(QJsonDocument::Compact);

qDebug() &lt;&lt; output; // prints "{\"id\":1,\"name\":\"hello\",\"sub\":{\"id\":2,\"name\":\"world\"}}" 
</code></pre>
]]></description><link>https://forum.qt.io/post/310112</link><guid isPermaLink="true">https://forum.qt.io/post/310112</guid><dc:creator><![CDATA[the_]]></dc:creator><pubDate>Thu, 28 Jan 2016 15:09:06 GMT</pubDate></item></channel></rss>