{"id":8483,"date":"2024-02-01T12:18:42","date_gmt":"2024-02-01T03:18:42","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=8483"},"modified":"2024-02-01T12:55:29","modified_gmt":"2024-02-01T03:55:29","slug":"c-nlohmannjson-%ea%b5%ac%ec%a1%b0%ec%b2%b4-json-%eb%b3%80%ed%99%98%ed%95%98%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=8483","title":{"rendered":"C++ &#8211; nlohmann::json \uad6c\uc870\uccb4-json \ubcc0\ud658\ud558\uae30"},"content":{"rendered":"<h1>C++ &#8211; nlohmann::json \uad6c\uc870\uccb4-json \ubcc0\ud658\ud558\uae30<\/h1>\n<h2>\uc124\uce58\ubc95<\/h2>\n<p>\ud504\ub85c\uc81d\ud2b8\uc5d0 \ud30c\uc77c\ud558\ub098 \ucd94\uac00\ud574\uc8fc\ub294 \uac83\uc73c\ub85c \uc124\uce58\uac00 \ub05d\ub09c\ub2e4.<\/p>\n<p><a href=\"https:\/\/github.com\/nlohmann\/json\/blob\/develop\/single_include\/nlohmann\/json.hpp\">\uc694\uae30<\/a><\/p>\n<h2>\uc0ac\uc6a9\ubc95<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;string&gt;\n#include &lt;iostream&gt;\nusing namespace std;\n\n#include &lt;fstream&gt;\n#include &quot;json.hpp&quot;\nusing json = nlohmann::json;<\/code><\/pre>\n<pre><code class=\"language-cpp\">struct Person {\n    string name;\n    int age;\n}<\/code><\/pre>\n<p>\ub450\uac1c\uc758 \ud568\uc218\ub97c \uc0dd\uc131\ud574 \uc90d\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-cpp\">void to_json(json&amp; j, const Person&amp; p) {\n    j = json{\n         {&quot;name&quot;, p.name}\n         , {&quot;age&quot;, p.age}\n    };\n}\n\nvoid from_json(const json&amp; j, Person&amp; p) {\n    j.at(&quot;name&quot;).get_to(p.name);\n    j.at(&quot;age&quot;).get_to(p.age);\n}<\/code><\/pre>\n<pre><code class=\"language-cpp\">\\int main(void) {\n    \/\/ save to file\n    Person p = {&quot;skyer9&quot;, 15};\n    json jf = p;\n    std::ofstream file(&quot;D:\/gitrepo\/test\/test.json&quot;);\n    file &lt;&lt; jf;\n    file.flush();\n\n    \/\/ read from file\n    std::ifstream ifs(&quot;D:\/gitrepo\/test\/test.json&quot;);\n    json jf2 = json::parse(ifs);\n    Person p2 = jf2;\n\n    cout &lt;&lt; p2.name &lt;&lt; endl;\n    cout &lt;&lt; p2.age &lt;&lt; endl;\n\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++ &#8211; nlohmann::json \uad6c\uc870\uccb4-json \ubcc0\ud658\ud558\uae30 \uc124\uce58\ubc95 \ud504\ub85c\uc81d\ud2b8\uc5d0 \ud30c\uc77c\ud558\ub098 \ucd94\uac00\ud574\uc8fc\ub294 \uac83\uc73c\ub85c \uc124\uce58\uac00 \ub05d\ub09c\ub2e4. \uc694\uae30 \uc0ac\uc6a9\ubc95 #include &lt;string&gt; #include &lt;iostream&gt; using namespace std; #include &lt;fstream&gt; #include &quot;json.hpp&quot; using json = nlohmann::json; struct Person { string name; int age; } \ub450\uac1c\uc758 \ud568\uc218\ub97c \uc0dd\uc131\ud574 \uc90d\ub2c8\ub2e4. void to_json(json&amp; j, const Person&amp; p) { j = json{ {&quot;name&quot;, p.name} , {&quot;age&quot;,\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=8483\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-8483","post","type-post","status-publish","format-standard","hentry","category-c-c"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8483"}],"version-history":[{"count":2,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8483\/revisions"}],"predecessor-version":[{"id":8485,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8483\/revisions\/8485"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}