wordreference/libjson/Getting Started/Library Interface/child_node.htm

50 lines
No EOL
2.7 KiB
HTML

<HTML>
<HEAD>
<title>libjson Child Node Example</title>
<script type="text/javascript" src="scripts/shCore.js"></script>
<script type="text/javascript" src="scripts/shBrushCpp.js"></script>
<link type="text/css" rel="stylesheet" href="styles/shCoreDefault.css"/>
<script type="text/javascript">SyntaxHighlighter.all();</script>
</HEAD>
<BODY>
<h1>libjson Child Node Example</h1>
<p>Now we&#39;ll look at how to create a branch off the main tree.</p>
<p><span id="more-114"></span></p>
<pre class="brush:cpp;wrap-lines:true">JSONNODE *n = json_new(JSON_NODE);
json_push_back(n, json_new_a(&quot;RootA&quot;, &quot;Value in parent node&quot;));
JSONNODE *c = json_new(JSON_NODE);
json_set_name(c, &quot;ChildNode&quot;);
json_push_back(c, json_new_a(&quot;ChildA&quot;, &quot;String Value&quot;));
json_push_back(c, json_new_i(&quot;ChildB&quot;, 42));
json_push_back(n, c);
json_char *jc = json_write_formatted(n);
printf(&quot;%s\n&quot;, jc);
json_free(jc);
json_delete(n);</pre>
<p>The result will look like this:</p>
<pre class="brush:cpp;wrap-lines:true">{
&quot;RootA&quot; : &quot;Value in parent node&quot;,
&quot;ChildNode&quot; : {
&quot;ChildA&quot; : &quot;String Value&quot;,
&quot;ChildB&quot; : 42
}
}</pre>
<p>As in the previous example, we create a root node to hold all of our child nodes on line one using <em>json_new</em>.</p>
<p>Line 2 adds to the root a string node with the name &quot;RootA&quot; and a value of &quot;Value in parent node&quot;.</p>
<p>On line 3 we&#39;re creating a new, floating node that will be a branch off the root. It&#39;s the same type as our root, JSON_NODE.</p>
<p><em>json_set_name</em> lets us name the node that we&#39;ve just created. If your branch is an object of some kind, you&#39;ll likely want to name it properly. Note that if you try to name the root node (<em>n</em> in our case), it won&#39;t be written out. The name will only appear if the JSON_NODE is a child of another node.</p>
<p>Lines 5 and 6 add new nodes with values to our branch node <em>c</em>.</p>
<p>Line 7 attaches our branch node <em>c</em> to the root node <em>n</em> after the string node named &quot;RootA&quot;. Note that if you fail to attach the branch to another node, you&#39;ll have to delete it manually or risk a memory leak.</p>
<p>Line 8 returns the <em>json_char</em> string nicely formatted and 9 prints it to stdout.</p>
<p>Line 10 frees our JSON string and line 11 loops through our root tree and frees up the memory we&#39;ve used.</p>
<p>With these tools, we can create complicated JSON structures and mimick objects with child properties.</p>
<p>Chris Larsen 2010-10-08</p>
<BODY>
</HTML>