文档章节

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

wzugang
 wzugang
发布于 2016/03/03 15:44
字数 560
阅读 328
收藏 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++对象...

黄泓凯
2019/03/25
968
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

没有更多内容

加载失败,请刷新页面

加载更多

整数反转(力扣)

整数反转 一、题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 ...

迪迪迪王
21分钟前
7
0
网页 大文件分片上传处理

总结一下大文件分片上传和断点续传的问题。因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况。http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议...

东方雨
23分钟前
8
0
MySQL开启连接次数限制(CONNECTION_CONTROL插件)

目前我们在使用MySQL的时候,没有对连接次数进行限制,如果碰到一些非法IP频繁进行root账号的撞击密码登陆时,不仅会影响MySLQ性能,而且会生成很多垃圾日志。 MySQL起使已经提供了连接控制的...

HarmonyChang
36分钟前
6
0
将Unicode字符串转换为Python中的字符串(包含多余的符号)

如何将Unicode字符串(包含额外的字符,如£$等)转换为Python字符串? #1楼 title = u"Klüft skräms inför på fédéral électoral große"import unicodedataunicodedata.normalize......

javail
38分钟前
4
0
Cinema 4D R20 for Mac(c4d r20三维动画设计渲染软件) 中文版

[名称]:Cinema 4D R20 for Mac [大小]: 390.2 MB [语言]:中文 [测试环境]: Mac OS 10.15.x [下载链接]:https://www.macdown.com/mac/301.html 简介 Cinema 4D R20完整版是一款专业的三维...

麦克虾仔
51分钟前
8
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部