文档章节

一个C语言json库,支持解析和构造,支持windows和linux

wzugang
 wzugang
发布于 2016/03/03 15:44
字数 560
阅读 323
收藏 3

<p>该json库是一个cJSON的改进版本。</p> <p>例程</p> <p>#include &quot;../json.h&quot;</p> <p>void doit(char *text) <br />{ <br />&#160;&#160;&#160; char *out;json_ht json; <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; json=json_parse(text); <br />&#160;&#160;&#160; if (!json) {printf(&quot;Error before: [%s]\n&quot;,json_error_get());} <br />&#160;&#160;&#160; else <br />&#160;&#160;&#160; { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; out=json_print(json,1); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; printf(&quot;%s\n&quot;,out); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; free(out); <br />&#160;&#160;&#160; } <br />&#160;&#160;&#160; json_delete(json); <br />}</p> <p>void dofile(char *filename) <br />{ <br />&#160;&#160;&#160; FILE *f;long len;char *data;&#160;&#160;&#160; <br />&#160;&#160;&#160; f=fopen(filename,&quot;rb&quot;);fseek(f,0,SEEK_END);len=ftell(f);fseek(f,0,SEEK_SET); <br />&#160;&#160;&#160; data=(char*)malloc(JALIGN(len+1,4));fread(data,1,len,f);fclose(f); <br />&#160;&#160;&#160; doit(data); <br />&#160;&#160;&#160; free(data); <br />}</p> <p>struct record <br />{ <br />&#160;&#160;&#160; const char *precision; <br />&#160;&#160;&#160; double lat,lon; <br />&#160;&#160;&#160; const char *address,*city,*state,*zip,*country; <br />};</p> <p>void create_objects() <br />{ <br />&#160;&#160;&#160; json_ht root,fmt,img,thm,fld;char *out;int i; <br />&#160;&#160;&#160; const char *strings[7]={&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;}; <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}}; <br />&#160;&#160;&#160; int ids[4]={116,943,234,38793}; <br />&#160;&#160;&#160; struct record fields[2]= <br />&#160;&#160;&#160; { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {&quot;zip&quot;,37.7668,-1.223959e+2,&quot;&quot;,&quot;SAN FRANCISCO&quot;,&quot;CA&quot;,&quot;94107&quot;,&quot;US&quot;}, <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {&quot;zip&quot;,37.371991,-1.22026e+2,&quot;&quot;,&quot;SUNNYVALE&quot;,&quot;CA&quot;,&quot;94085&quot;,&quot;US&quot;} <br />&#160;&#160;&#160; };</p> <p>&#160;&#160;&#160; root=json_object_new();&#160;&#160;&#160; <br />&#160;&#160;&#160; json_object_add(root, &quot;name&quot;, json_string_new(&quot;Jack (\&quot;Bee\&quot;) Nimble&quot;)); <br />&#160;&#160;&#160; json_object_add(root, &quot;format&quot;, fmt=json_object_new()); <br />&#160;&#160;&#160; json_object_add(fmt,&quot;type&quot;,json_string_new(&quot;rect&quot;)); <br />&#160;&#160;&#160; json_object_add(fmt,&quot;width&quot;,json_number_new(1920)); <br />&#160;&#160;&#160; json_object_add(fmt,&quot;height&quot;,json_number_new(1080)); <br />&#160;&#160;&#160; json_object_add (fmt,&quot;interlace&quot;,json_false_new()); <br />&#160;&#160;&#160; json_object_add(fmt,&quot;frame rate&quot;,json_number_new(24)); <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; out=json_print(root,1);&#160;&#160;&#160; <br />&#160;&#160;&#160; json_delete(root);&#160;&#160;&#160; <br />&#160;&#160;&#160; printf(&quot;%s\n&quot;,out);&#160;&#160;&#160; <br />&#160;&#160;&#160; free(out);</p> <p>&#160;&#160;&#160; root=json_array_string_new(strings,7); <br />&#160;&#160;&#160; out=json_print(root,1);&#160;&#160;&#160; <br />&#160;&#160;&#160; json_delete(root);&#160;&#160;&#160; <br />&#160;&#160;&#160; printf(&quot;%s\n&quot;,out);&#160;&#160;&#160; <br />&#160;&#160;&#160; free(out);</p> <p>&#160;&#160;&#160; root=json_array_new(); <br />&#160;&#160;&#160; for (i=0;i&lt;3;i++) <br />&#160;&#160;&#160; { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_array_add(root,json_array_int_new(numbers[i],3)); <br />&#160;&#160;&#160; } <br />&#160;&#160;&#160; out=json_print(root,1);&#160;&#160;&#160; <br />&#160;&#160;&#160; json_delete(root);&#160;&#160;&#160; <br />&#160;&#160;&#160; printf(&quot;%s\n&quot;,out);&#160;&#160;&#160; <br />&#160;&#160;&#160; free(out);</p> <p>&#160;&#160;&#160; root=json_object_new(); <br />&#160;&#160;&#160; json_object_add(root, &quot;Image&quot;, img=json_object_new()); <br />&#160;&#160;&#160; json_object_add(img,&quot;Width&quot;,json_number_new(800)); <br />&#160;&#160;&#160; json_object_add(img,&quot;Height&quot;,json_number_new(600)); <br />&#160;&#160;&#160; json_object_add(img,&quot;Title&quot;,json_string_new(&quot;View from 15th Floor&quot;)); <br />&#160;&#160;&#160; json_object_add(img, &quot;Thumbnail&quot;, thm=json_object_new()); <br />&#160;&#160;&#160; json_object_add(thm, &quot;Url&quot;, json_string_new(&quot;http:/*www.example.com/image/481989943&quot;)); <br />&#160;&#160;&#160; json_object_add_number(thm,&quot;Height&quot;,125); <br />&#160;&#160;&#160; json_object_add(thm,&quot;Width&quot;,json_string_new(&quot;100&quot;)); <br />&#160;&#160;&#160; json_object_add(img,&quot;IDs&quot;, json_array_int_new(ids,4)); <br />&#160;&#160;&#160; out=json_print(root,1);&#160;&#160;&#160; <br />&#160;&#160;&#160; json_delete(root);&#160;&#160;&#160; <br />&#160;&#160;&#160; printf(&quot;%s\n&quot;,out);&#160;&#160;&#160; <br />&#160;&#160;&#160; free(out);</p> <p>&#160;&#160;&#160; root=json_array_new(); <br />&#160;&#160;&#160; for (i=0;i&lt;2;i++) <br />&#160;&#160;&#160; { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_array_add(root,fld=json_object_new()); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_object_add(fld, &quot;precision&quot;, json_string_new(fields[i].precision)); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_object_add(fld, &quot;Latitude&quot;, json_number_new(fields[i].lat)); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_object_add(fld, &quot;Longitude&quot;, json_number_new(fields[i].lon)); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_object_add(fld, &quot;Address&quot;, json_string_new(fields[i].address)); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_object_add(fld, &quot;City&quot;, json_string_new(fields[i].city)); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_object_add(fld, &quot;State&quot;, json_string_new(fields[i].state)); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_object_add(fld, &quot;Zip&quot;, json_string_new(fields[i].zip)); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_object_add(fld, &quot;Country&quot;, json_string_new(fields[i].country)); <br />&#160;&#160;&#160; }&#160;&#160;&#160; <br />&#160;&#160;&#160; //json_object_replace(json_array_get(root,1),&quot;City&quot;,json_int_array(ids,4));&#160;&#160;&#160; <br />&#160;&#160;&#160; out=json_print(root,1);&#160;&#160;&#160; <br />&#160;&#160;&#160; json_delete(root);&#160;&#160;&#160; <br />&#160;&#160;&#160; printf(&quot;%s\n&quot;,out);&#160;&#160;&#160; <br />&#160;&#160;&#160; free(out);</p> <p>}</p> <p>void test() <br />{ <br />&#160;&#160;&#160; json_ht pJsonRoot = NULL,tf=NULL;</p> <p>&#160;&#160;&#160; pJsonRoot = json_object_new(); <br />&#160;&#160;&#160; if(NULL == pJsonRoot) <br />&#160;&#160;&#160; { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; return ; <br />&#160;&#160;&#160; } <br />&#160;&#160;&#160; json_object_add_string(pJsonRoot, &quot;hello&quot;, &quot;hello world&quot;); <br />&#160;&#160;&#160; json_object_add_number(pJsonRoot, &quot;number&quot;, 10010); <br />&#160;&#160;&#160; json_object_add_bool(pJsonRoot, &quot;bool&quot;, 1); <br />&#160;&#160;&#160; json_ht pSubJson = NULL; <br />&#160;&#160;&#160; pSubJson = json_object_new(); <br />&#160;&#160;&#160; if(NULL == pSubJson) <br />&#160;&#160;&#160; { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_delete(pJsonRoot); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; return ; <br />&#160;&#160;&#160; } <br />&#160;&#160;&#160; json_object_add_string(pSubJson, &quot;subjsonobj&quot;, &quot;a sub json string&quot;); <br />&#160;&#160;&#160; json_object_add(pJsonRoot, &quot;subobj&quot;, pSubJson); <br />&#160;&#160;&#160; json_saveto_file(pJsonRoot,&quot;test1.json&quot;); <br />&#160;&#160;&#160; json_delete(pJsonRoot); <br />&#160;&#160;&#160; printf(&quot;==========================================json_parse_file====================================================\n&quot;); <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; tf = json_parse_file(&quot;test1.json&quot;); <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; char * p = json_print(tf,1); <br />&#160;&#160;&#160; if(NULL == p) <br />&#160;&#160;&#160; { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; json_delete(tf); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; return ; <br />&#160;&#160;&#160; } <br />&#160;&#160;&#160; printf(&quot;%s\n&quot;, p); <br />&#160;&#160;&#160; printf(&quot;============================================================================================================\n&quot;); <br />&#160;&#160;&#160; json_delete(tf); <br />}</p> <p>int main (int argc, const char * argv[]) <br />{ <br />&#160;&#160;&#160; char text1[]=&quot;{\n\&quot;name\&quot;: \&quot;Jack (\\\&quot;Bee\\\&quot;) Nimble\&quot;, \n\&quot;format\&quot;: {\&quot;type\&quot;:&#160;&#160;&#160;&#160;&#160;&#160; \&quot;rect\&quot;, \n\&quot;width\&quot;:&#160;&#160;&#160;&#160;&#160; 1920, \n\&quot;height\&quot;:&#160;&#160;&#160;&#160; 1080, \n\&quot;interlace\&quot;:&#160; false,\&quot;frame rate\&quot;: 24\n}\n}&quot;;&#160;&#160;&#160; <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; doit(text1); <br />&#160;&#160;&#160; test(); <br />&#160;&#160;&#160; create_objects(); <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; return 0; <br />}</p> <p> <br />官方博客:http://www.wangzugang.net/blog/json.html</p> <p> <br />源码地址:https://github.com/wzugang/json</p>

© 著作权归作者所有

wzugang

wzugang

粉丝 16
博文 4
码字总数 1771
作品 0
杭州
架构师
私信 提问
轻量简单的 C++ JSON 解析库 - ejson

介绍 该库为轻量级的c++ json解析与构造库,源码很短,适合学习,觉得不错的点个star。 提供了的功能: json字符串解析为c++对象(JSONArray和JSONObject) c++对象中获取key-value值 c++对象...

黄泓凯
03/25
857
0
acl 3.1.2 版本发布,网络通信与服务器编程框架

acl 3.1.2 版本发布了,acl 是 one advanced C/C++ library 的简称,主要包括网络通信库以及服务器框架库等功能,支持 Linux/Windows/Solaris/FreeBsd/MacOS 平台;整个 acl 项目主要包含三个...

郑树新
2015/05/11
1K
6
网络与服务器编程框架库 acl_3.0.19 发布

acl 3.0.19 版本 (项目主页:https://sourceforge.net/projects/acl/,技术文章主页:http://zsxxsz.iteye.com/ ) 发布了,acl 是 one advanced C/C++ library 的简称,主要包括网络通信库以...

郑树新
2014/06/23
1K
7
网络与服务器编程框架库 acl_3.0.14 发布

acl 3.0.14 版本 (项目主页:https://sourceforge.net/projects/acl/) 发布了,acl 是 one Advanced C/C++ library 的简称,主要包括网络通信库以及服务器框架库等功能,支持 Linux/Windows/...

郑树新
2013/12/07
2.5K
7
acl -- 网络及服务器编程框架库 3.0.15 版本发布

acl 3.0.15 版本 (项目主页:https://sourceforge.net/projects/acl/,技术文章主页:http://zsxxsz.iteye.com/)) 发布了,acl 是 one Advanced C/C++ library 的简称,主要包括网络通信库以...

郑树新
2014/01/25
1K
2

没有更多内容

加载失败,请刷新页面

加载更多

电脑数据加密用什么软件好?深圳源代码数据加密软件工具那个好?风奥科技

企业文件加密对于企业发展的重要性?现在科学技术的发展,信息化进程的加密快,信息化产品以及融入生活与我们的日常生活息息相关!例如个人用户在日常的网购、以及通过一定的设备来获取新闻等...

fasoft
29分钟前
4
0
redis运行一段时间之后假死

现象:redis运行一段时间之后就会出现假死的现象 假死指的是:进程在、pid在、telnet 端口是通的、redis-cli命令可以连接,但无法查询数据,查询的时候报错,如下图: 可以看到这个报错是有关...

Jack088
32分钟前
5
0
truncate被Waiting for table metadata lock的解决方法

场景 调研环境下,对一张千万条数据的表做了一个truncate操作,发现长时间无反应。 解决思路 操作一直执行,决定查询下正在执行的sql的状态 shop processlist; 发现truncate操作的状态是:W...

XuePeng77
今天
6
0
在PostgreSQL中显示表格

在PostgreSQL中show tables (从MySQL)相当于什么? #1楼 使用-E标志运行psql将回显内部用于实现\\ dt和类似内容的查询: sudo -u postgres psql -Epostgres=# \dt ********* QUER...

技术盛宴
今天
6
0
osi七层模型

OSI 七层模型和TCP/IP模型及对应协议(详解) 完成中继功能的节点通常称为中继系统。在OSI七层模型中,处于不同层的中继系统具有不同的名称。 一个设备工作在哪一层,关键看它工作时利用哪一...

xpp_ba
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部