{"id":3559,"date":"2017-08-19T17:56:05","date_gmt":"2017-08-19T09:56:05","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3559"},"modified":"2017-08-19T17:56:05","modified_gmt":"2017-08-19T09:56:05","slug":"linux%e4%b8%8bc%e8%af%ad%e8%a8%80%e6%93%8d%e4%bd%9cjson%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3559.html","title":{"rendered":"Linux\u4e0bC\u8bed\u8a00\u64cd\u4f5cjson\u6570\u636e"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u603b\u7ed3\u3001\u63d0\u9ad8\u9700\u8981<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<pre class=\"lang:default decode:true\">#include &lt;stdio.h&gt;\r\n#include &lt;json-c\/json.h&gt;\r\n\r\n\/*\r\n{\r\n  Name: haha,\r\n  Id:   101,\r\n  Age:  21,\r\n  info:{\r\n    number:  1,\r\n    score:  91,\r\n    type:    2,\r\n    params: \"\"\r\n  }\r\n}\r\n*\/\r\n\r\n\/\/ gcc json-c_parse_string.c -ljson-c\r\nint main(int argc, char const *argv[])\r\n{\r\n  \/* Declaring the json data's in json format. *\/\r\n  char buf[] = \"{ \\\"Name\\\": \\\"haha\\\", \\\"Id\\\": 101, \\\"Age\\\": 21, \\\"info\\\": { \\\"number\\\": 1, \\\"score\\\": 91, \\\"type\\\": 2, \\\"params\\\": \\\"w\\\" } }\";\r\n\r\n  \/* Declaring the json_object. To pass the Json string to the newly created json_object. *\/\r\n  json_object *new_obj = json_tokener_parse(buf);\r\n  if (!new_obj)\r\n    return -1;\r\n\r\n  json_object *val_obj = NULL;\r\n  json_object *result = NULL;\r\n  const char *str = NULL;\r\n\r\n  \/* To get the data's then we have to get to the specific node by using the below function. *\/\r\n\r\n  if( json_object_object_get_ex(new_obj, \"Name\", &amp;val_obj) ) {\r\n    str = json_object_get_string(val_obj);\r\n    printf(\"Name: %s\\n\", str);\r\n  }\r\n  if( json_object_object_get_ex(new_obj, \"Id\", &amp;val_obj) ) {\r\n    str = json_object_get_string(val_obj);\r\n    printf(\"Id: %s\\n\", str);\r\n  }\r\n  if( json_object_object_get_ex(new_obj, \"Age\", &amp;val_obj) ) {\r\n    str = json_object_get_string(val_obj);\r\n    printf(\"Age: %s\\n\", str);\r\n  }\r\n\r\n  if( json_object_object_get_ex(new_obj, \"info\", &amp;val_obj) ) {\r\n    if( json_object_object_get_ex(val_obj, \"number\", &amp;result) ) {\r\n      printf(\"\\tinfo -&gt; number: %d\\n\", json_object_get_int(result));\r\n    }\r\n    if( json_object_object_get_ex(val_obj, \"score\", &amp;result) ) {\r\n      printf(\"\\tinfo -&gt; score: %d\\n\", json_object_get_int(result));\r\n    }\r\n    if( json_object_object_get_ex(val_obj, \"type\", &amp;result) ) {\r\n      printf(\"\\tinfo -&gt; type: %d\\n\", json_object_get_int(result));\r\n    }\r\n    if( json_object_object_get_ex(val_obj, \"params\", &amp;result) ) {\r\n      printf(\"\\tinfo -&gt; params: %s\\n\", json_object_get_string(result));\r\n    }\r\n  }\r\n\r\n  json_object_put(new_obj); \/\/ to return the pointer to its originalobjects\r\n\r\n  return 0;\r\n}<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;stddef.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;json-c\/json.h&gt;\r\n\r\n#ifdef TEST_FORMATTED\r\n#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj, sflags)\r\n#else\r\n\/* no special define *\/\r\n#endif\r\n\r\n\/\/ gcc json_object_to_json_string.c -ljson-c -DTEST_FORMATTED=2\r\nint main(int argc, char **argv)\r\n{\r\n\tjson_object *new_obj;\r\n#ifdef TEST_FORMATTED\r\n\tint sflags = TEST_FORMATTED;\r\n#endif\r\n\r\n\tnew_obj = json_tokener_parse(\"\/* more difficult test case *\/\"\r\n\t\t\t\t     \"{ \\\"glossary\\\": { \\\"title\\\": \\\"example glossary\\\", \\\"GlossDiv\\\": { \\\"title\\\": \\\"S\\\", \\\"GlossList\\\": [ { \\\"ID\\\": \\\"SGML\\\", \\\"SortAs\\\": \\\"SGML\\\", \\\"GlossTerm\\\": \\\"Standard Generalized Markup Language\\\", \\\"Acronym\\\": \\\"SGML\\\", \\\"Abbrev\\\": \\\"ISO 8879:1986\\\", \\\"GlossDef\\\": \\\"A meta-markup language, used to create markup languages such as DocBook.\\\", \\\"GlossSeeAlso\\\": [\\\"GML\\\", \\\"XML\\\", \\\"markup\\\"] } ] } } }\");\r\n\tprintf(\"new_obj.to_string() = %s\\n\", json_object_to_json_string(new_obj));\r\n\tjson_object_put(new_obj);\r\n\r\n\treturn 0;\r\n}<\/pre>\n<hr \/>\n<p>json_object_new_object();<br \/>\njson_object_new_int();<br \/>\njson_object_new_string();<br \/>\njson_object_object_add();<\/p>\n<p>json_object_to_json_string();<\/p>\n<p>json_tokener_parse();<br \/>\njson_object_object_get_ex();<br \/>\njson_object_get_int();<br \/>\njson_object_get_string();<\/p>\n<p>json_object_put();<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<p>json-c<br \/>\n<a href=\"https:\/\/github.com\/json-c\/json-c\">https:\/\/github.com\/json-c\/json-c<\/a><br \/>\n<a href=\"https:\/\/github.com\/json-c\/json-c\/tree\/master\/tests\">https:\/\/github.com\/json-c\/json-c\/tree\/master\/tests<\/a><br \/>\n<a href=\"https:\/\/json-c.github.io\/json-c\/json-c-0.12\/doc\/html\/json__object_8h.html\">https:\/\/json-c.github.io\/json-c\/json-c-0.12\/doc\/html\/json__object_8h.html<\/a>\u00a0#<span style=\"color: #0000ff;\"><strong>\u5b98\u65b9\u6587\u6863<\/strong><\/span><\/p>\n<p><span style=\"color: #ff0000;\"><strong>JSON C\u8bed\u8a00API\u6574\u7406<\/strong><\/span> #nice<br \/>\n<a href=\"http:\/\/7player.cn\/2015\/01\/03\/json-c%E8%AF%AD%E8%A8%80api%E6%95%B4%E7%90%86\/\">http:\/\/7player.cn\/2015\/01\/03\/json-c%E8%AF%AD%E8%A8%80api%E6%95%B4%E7%90%86\/<\/a><\/p>\n<p>Parsing JSON using C [closed]<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/6673936\/parsing-json-using-c\">https:\/\/stackoverflow.com\/questions\/6673936\/parsing-json-using-c<\/a><\/p>\n<p>How to extract value of json object key, value pairs using json-c<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/29483115\/how-to-extract-value-of-json-object-key-value-pairs-using-json-c\">https:\/\/stackoverflow.com\/questions\/29483115\/how-to-extract-value-of-json-object-key-value-pairs-using-json-c<\/a><\/p>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u603b\u7ed3\u3001\u63d0\u9ad8\u9700\u8981 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a #include &lt;stdio.h&gt;  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[100,181,921,30],"class_list":["post-3559","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-c","tag-json","tag-json-c","tag-linux"],"views":9353,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3559"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3559\/revisions"}],"predecessor-version":[{"id":3566,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3559\/revisions\/3566"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}