<?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[Accessing json data from .qrc file using std::ifstream and nlohmann json]]></title><description><![CDATA[<p dir="auto">I am trying to parse a json file, placed in the .qrc directory of my project. When I use QFile and QJsonDocument, and QJsonObject; it works fine. But, when I try to use std::ifstream and nlohmann::json, the file is not parsed.<br />
Also, if::stream and nlohmann::json function parses the file if I give a hard coded path, instead of including the file in the .qrc folder of the project.<br />
I am confident about the format of my json file.<br />
So, is there any way to parse the json file, present in the .qrc directory using std::ifstream and nlohmann::json?</p>
<p dir="auto">This is how I am parsing the json data:</p>
<pre><code>std::ifstream file1(jsonFilePath1);
jsonObject1 = nlohmann::json::parse(file1);
</code></pre>
<p dir="auto">This is the exception I am getting:<br />
terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_3::detail::type_error'<br />
what():  [json.exception.type_error.304] cannot use at() with null</p>
]]></description><link>https://forum.qt.io/topic/156327/accessing-json-data-from-qrc-file-using-std-ifstream-and-nlohmann-json</link><generator>RSS for Node</generator><lastBuildDate>Mon, 18 May 2026 08:43:36 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/156327.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 26 Apr 2024 06:59:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Accessing json data from .qrc file using std::ifstream and nlohmann json on Fri, 26 Apr 2024 07:16:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/paul-colby">@<bdi>Paul-Colby</bdi></a><br />
Thank you so much for such a detailed solution!<br />
I think I would go with option 3 in my case.</p>
]]></description><link>https://forum.qt.io/post/798098</link><guid isPermaLink="true">https://forum.qt.io/post/798098</guid><dc:creator><![CDATA[shreya_agrawal]]></dc:creator><pubDate>Fri, 26 Apr 2024 07:16:49 GMT</pubDate></item><item><title><![CDATA[Reply to Accessing json data from .qrc file using std::ifstream and nlohmann json on Fri, 26 Apr 2024 07:10:46 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/shreya_agrawal">@<bdi>shreya_agrawal</bdi></a>,</p>
<p dir="auto">As <a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a> said, only QFIle knows how to access files embedded via <a href="https://doc.qt.io/qt-5/resources.html" target="_blank" rel="noopener noreferrer nofollow ugc">the Qt Resource System</a>, so your options are:</p>
<ol>
<li>Use <a href="https://doc.qt.io/qt-6/qfile.html#copy-1" target="_blank" rel="noopener noreferrer nofollow ugc">QFile::copy()</a> to copy the file out of qrc, onto disk somewhere; or</li>
<li>Use QFile to read the contents of the file, then pass the contents to <code>nlohmann::json::parse()</code> (there's an overload the accepts a <code>std::string::iterator</code>, for example); or</li>
<li>If you <em>really</em> need to parse in a streaming mode, with <code>nlohmann::json</code>, then you'll need to use a <code>QFile</code> wrapper that implements <code>std::istream</code>; something like this: <a href="https://stackoverflow.com/questions/5204335/how-to-use-a-qfile-with-stdiostream#answers" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/5204335/how-to-use-a-qfile-with-stdiostream#answers</a></li>
</ol>
<p dir="auto">Cheers.</p>
]]></description><link>https://forum.qt.io/post/798095</link><guid isPermaLink="true">https://forum.qt.io/post/798095</guid><dc:creator><![CDATA[Paul Colby]]></dc:creator><pubDate>Fri, 26 Apr 2024 07:10:46 GMT</pubDate></item><item><title><![CDATA[Reply to Accessing json data from .qrc file using std::ifstream and nlohmann json on Fri, 26 Apr 2024 07:04:38 GMT]]></title><description><![CDATA[<p dir="auto">Qrc files are only accessible by Qt classes. How should <code>ifstream</code> know how to read a qrc file?<br />
You need to read the file using Qt classes and pass the content data to other classes.</p>
]]></description><link>https://forum.qt.io/post/798093</link><guid isPermaLink="true">https://forum.qt.io/post/798093</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Fri, 26 Apr 2024 07:04:38 GMT</pubDate></item></channel></rss>