文档章节

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

wzugang
 wzugang
发布于 2016/03/03 15:44
字数 560
阅读 342
收藏 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
杭州
架构师
私信 提问
加载中

评论(0)

轻量简单的 C++ JSON 解析库 - ejson

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

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

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

郑树新
2015/05/11
1.6K
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
1.5K
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
1.3K
2

没有更多内容

加载失败,请刷新页面

加载更多

REST API服务为验证失败返回的适当HTTP状态代码是什么?

每当我在基于Django / Piston的REST API应用程序中遇到验证失败时,我目前正在返回401 Unauthorized。 看过HTTP状态代码注册表后我不相信这是验证失败的合适代码,你们都推荐什么? 400错误请...

javail
31分钟前
76
0
《计算机程序的构造和解释》分享下载

书籍信息 书名:《计算机程序的构造和解释》 原作名:Structure and Interpretation of Computer Programs 作者: Harold Abelson / Gerald Jay Sussman / Julie Sussman 豆瓣评分:9.5分(22...

开始以后_
36分钟前
75
0
《Linux就该这么学》第六节课while循环语句,case测试语句,计划任务及用户文件的相关命令

《Linux就该这么学》 本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材,目前是国内最值得去读的Lin...

溪风之殇
45分钟前
60
0
有没有办法让非root进程绑定到Linux上的“特权”端口?

在我的开发盒上有这个限制是非常烦人的,因为除了我之外不会有任何用户。 我知道标准的解决方法 ,但它们都没有完全符合我的要求: authbind (Debian测试中的版本,1.0,仅支持IPv4) 使用i...

技术盛宴
46分钟前
55
0
Java程序员必须要了解的类Unsafe

前言 Java是一个安全的编程语言,它能最大程度的防止程序员犯一些低级的错误(大部分是和内存管理有关的)。但凡是不是绝对的,使用Unsafe程序员就可以操作内存,因此可能带来一个安全隐患。...

Onegoleya
46分钟前
52
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部