child_node.htm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <HTML>
  2. <HEAD>
  3. <title>libjson Child Node Example</title>
  4. <script type="text/javascript" src="scripts/shCore.js"></script>
  5. <script type="text/javascript" src="scripts/shBrushCpp.js"></script>
  6. <link type="text/css" rel="stylesheet" href="styles/shCoreDefault.css"/>
  7. <script type="text/javascript">SyntaxHighlighter.all();</script>
  8. </HEAD>
  9. <BODY>
  10. <h1>libjson Child Node Example</h1>
  11. <p>Now we&#39;ll look at how to create a branch off the main tree.</p>
  12. <p><span id="more-114"></span></p>
  13. <pre class="brush:cpp;wrap-lines:true">JSONNODE *n = json_new(JSON_NODE);
  14. json_push_back(n, json_new_a(&quot;RootA&quot;, &quot;Value in parent node&quot;));
  15. JSONNODE *c = json_new(JSON_NODE);
  16. json_set_name(c, &quot;ChildNode&quot;);
  17. json_push_back(c, json_new_a(&quot;ChildA&quot;, &quot;String Value&quot;));
  18. json_push_back(c, json_new_i(&quot;ChildB&quot;, 42));
  19. json_push_back(n, c);
  20. json_char *jc = json_write_formatted(n);
  21. printf(&quot;%s\n&quot;, jc);
  22. json_free(jc);
  23. json_delete(n);</pre>
  24. <p>The result will look like this:</p>
  25. <pre class="brush:cpp;wrap-lines:true">{
  26. &quot;RootA&quot; : &quot;Value in parent node&quot;,
  27. &quot;ChildNode&quot; : {
  28. &quot;ChildA&quot; : &quot;String Value&quot;,
  29. &quot;ChildB&quot; : 42
  30. }
  31. }</pre>
  32. <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>
  33. <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>
  34. <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>
  35. <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>
  36. <p>Lines 5 and 6 add new nodes with values to our branch node <em>c</em>.</p>
  37. <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>
  38. <p>Line 8 returns the <em>json_char</em> string nicely formatted and 9 prints it to stdout.</p>
  39. <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>
  40. <p>With these tools, we can create complicated JSON structures and mimick objects with child properties.</p>
  41. <p>Chris Larsen 2010-10-08</p>
  42. <BODY>
  43. </HTML>