JSON

[C/C++]C语言json处理库

字号+ 作者:H5之家 来源:H5之家 2018-02-11 13:06 我要评论( )

该程序是在cJSON的基础上进行了部分修改优化,并在原有的基础上做了一些扩展。提供windows的dll动态库、头文件、测试例程、Makefile等。该dll使用mingw编译,vc可以使用LoadLibrary动态加载里面的函数,mingw可以直接调用(链接dll文件就可以)。程序有些部

该程序是在cJSON的基础上进行了部分修改优化,并在原有的基础上做了一些扩展。提供windows的dll动态库、头文件、测试例程、Makefile等。该dll使用mingw编译,vc可以使用LoadLibrary动态加载里面的函数,mingw可以直接调用(链接dll文件就可以)。程序有些部分不够完善,以后还会继续改进。 scrolltotop.offset(100,120); scrolltotop.init(); [1].[代码] [C/C++]代码 #include "../json.h" void doit(char *text) { char *out;json_ht json; json=json_parse(text); if (!json) {printf("Error before: [%s]\n",json_error_get());} else { out=json_print(json,1); printf("%s\n",out); free(out); } json_destroy(json); } void dofile(char *filename) { FILE *f;long len;char *data; f=fopen(filename,"rb");fseek(f,0,SEEK_END);len=ftell(f);fseek(f,0,SEEK_SET); data=(char*)malloc(JALIGN(len+1,4));fread(data,1,len,f);fclose(f); doit(data); free(data); } struct record { const char *precision; double lat,lon; const char *address,*city,*state,*zip,*country; }; void create_objects() { json_ht root,fmt,img,thm,fld;char *out;int i; const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}}; int ids[4]={116,943,234,38793}; struct record fields[2]= { {"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"}, {"zip",37.371991,-1.22026e+2,"","SUNNYVALE","CA","94085","US"} }; root=json_object(); json_object_add(root, "name", json_string("Jack (\"Bee\") Nimble")); json_object_add(root, "format", fmt=json_object()); json_object_add(fmt,"type",json_string("rect")); json_object_add(fmt,"width",json_number(1920)); json_object_add(fmt,"height",json_number(1080)); json_object_add (fmt,"interlace",json_false()); json_object_add(fmt,"frame rate",json_number(24)); out=json_print(root,1); json_destroy(root); printf("%s\n",out); free(out); root=json_array_string(strings,7); out=json_print(root,1); json_destroy(root); printf("%s\n",out); free(out); root=json_array(); for (i=0;i<3;i++) { json_array_add(root,json_array_int(numbers[i],3)); } out=json_print(root,1); json_destroy(root); printf("%s\n",out); free(out); root=json_object(); json_object_add(root, "Image", img=json_object()); //cJSON_AddNumberToObject(img,"Width",800); json_object_add(img,"Width",json_number(800)); json_object_add(img,"Height",json_number(600)); json_object_add(img,"Title",json_string("View from 15th Floor")); json_object_add(img, "Thumbnail", thm=json_object()); json_object_add(thm, "Url", json_string("http:/*")); json_object_add_number(thm,"Height",125); json_object_add(thm,"Width",json_string("100")); json_object_add(img,"IDs", json_array_int(ids,4)); out=json_print(root,1); json_destroy(root); printf("%s\n",out); free(out); root=json_array(); for (i=0;i<2;i++) { json_array_add(root,fld=json_object()); json_object_add(fld, "precision", json_string(fields[i].precision)); json_object_add(fld, "Latitude", json_number(fields[i].lat)); json_object_add(fld, "Longitude", json_number(fields[i].lon)); json_object_add(fld, "Address", json_string(fields[i].address)); json_object_add(fld, "City", json_string(fields[i].city)); json_object_add(fld, "State", json_string(fields[i].state)); json_object_add(fld, "Zip", json_string(fields[i].zip)); json_object_add(fld, "Country", json_string(fields[i].country)); } //json_object_replace(json_array_get(root,1),"City",json_int_array(ids,4)); out=json_print(root,1); json_destroy(root); printf("%s\n",out); free(out); } void test() { json_ht pJsonRoot = NULL,tf=NULL; pJsonRoot = json_object(); if(NULL == pJsonRoot) { return ; } json_object_add_string(pJsonRoot, "hello", "hello world"); json_object_add_number(pJsonRoot, "number", 10010); json_object_add_bool(pJsonRoot, "bool", 1); json_ht pSubJson = NULL; pSubJson = json_object(); if(NULL == pSubJson) { json_destroy(pJsonRoot); return ; } json_object_add_string(pSubJson, "subjsonobj", "a sub json string"); json_object_add(pJsonRoot, "subobj", pSubJson); json_saveto_file(pJsonRoot,"test1.json"); json_destroy(pJsonRoot); printf("==========================================json_parse_file====================================================\n"); tf = json_parse_file("test1.json"); char * p = json_print(tf,1); if(NULL == p) { json_destroy(tf); return ; } printf("%s\n", p); printf("============================================================================================================\n"); json_destroy(tf); } int main (int argc, const char * argv[]) { char text1[]="{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\": \"rect\", \n\"width\": 1920, \n\"height\": 1080, \n\"interlace\": false,\"frame rate\": 24\n}\n}"; //char text2[]="[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]"; //char text3[]="[\n [0, -1, 0],\n [1, 0, 0],\n [0, 0, 1]\n ]\n"; //char text4[]="{\n \"Image\": {\n \"Width\": 800,\n \"Height\": 600,\n \"Title\": \"View from 15th Floor\",\n \"Thumbnail\": {\n \"Url\": \"http:/*\",\n \"Height\": 125,\n \"Width\": \"100\"\n },\n \"IDs\": [116, 943, 234, 38793]\n }\n }"; //char text5[]="[\n {\n \"precision\": \"zip\",\n \"Latitude\": 37.7668,\n \"Longitude\": -122.3959,\n \"Address\": \"\",\n \"City\": \"SAN FRANCISCO\",\n \"State\": \"CA\",\n \"Zip\": \"94107\",\n \"Country\": \"US\"\n },\n {\n \"precision\": \"zip\",\n \"Latitude\": 37.371991,\n \"Longitude\": -122.026020,\n \"Address\": \"\",\n \"City\": \"SUNNYVALE\",\n \"State\": \"CA\",\n \"Zip\": \"94085\",\n \"Country\": \"US\"\n }\n ]"; // doit(text1); //doit(text2); //doit(text3); //doit(text4); //doit(text5); test(); create_objects(); return 0; } [2].[文件] json.zip ~ 48KB    (0) 文件不存在或者代码语言不存在 SyntaxHighlighter.autoloader( 'applescript /js/sh309/scripts/shBrushAppleScript.js?t=1445088768000', 'actionscript3 as3 /js/sh309/scripts/shBrushAS3.js?t=1445088768000', 'bash shell /js/sh309/scripts/shBrushBash.js?t=1445088768000', 'coldfusion cf /js/sh309/scripts/shBrushColdFusion.js?t=1445088768000', 'cpp c /js/sh309/scripts/shBrushCpp.js?t=1445088768000', 'obj-c objc /js/sh309/scripts/shBrushObjC.js?t=1445088768000', 'c# c-sharp csharp /js/sh309/scripts/shBrushCSharp.js?t=1445088768000', 'css /js/sh309/scripts/shBrushCss.js?t=1445088768000', 'delphi pascal /js/sh309/scripts/shBrushDelphi.js?t=1445088768000', 'diff patch pas /js/sh309/scripts/shBrushDiff.js?t=1445088768000', 'erl erlang /js/sh309/scripts/shBrushErlang.js?t=1445088768000', 'groovy /js/sh309/scripts/shBrushGroovy.js?t=1445088768000', 'haxe hx /js/sh309/scripts/shBrushHaxe.js?t=1445088768000', 'java /js/sh309/scripts/shBrushJava.js?t=1445088768000', 'jfx javafx /js/sh309/scripts/shBrushJavaFX.js?t=1445088768000', 'js jscript javascript /js/sh309/scripts/shBrushJScript.js?t=1445088768000', 'perl pl /js/sh309/scripts/shBrushPerl.js?t=1445088768000', 'php /js/sh309/scripts/shBrushPhp.js?t=1445088768000', 'text plain /js/sh309/scripts/shBrushPlain.js?t=1445088768000', 'py python /js/sh309/scripts/shBrushPython.js?t=1445088768000', 'ruby rails ror rb /js/sh309/scripts/shBrushRuby.js?t=1445088768000', 'scala /js/sh309/scripts/shBrushScala.js?t=1445088768000', 'sql /js/sh309/scripts/shBrushSql.js?t=1445088768000', 'vb vbnet /js/sh309/scripts/shBrushVb.js?t=1445088768000', 'xml xhtml xslt html /js/sh309/scripts/shBrushXml.js?t=1445088768000' ); SyntaxHighlighter.all();

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • JSON处理Hibernate实体类net.sf.json.JSONException: There is a

    JSON处理Hibernate实体类net.sf.json.JSONException: There is a

    2018-02-10 12:00

  • 给控制台程序传递json参数时出错

    给控制台程序传递json参数时出错

    2018-02-10 11:01

  • vizjson:json 中的图形语法

    vizjson:json 中的图形语法

    2018-02-10 08:00

  • 转换JSONArray到字符串

    转换JSONArray到字符串

    2018-02-09 18:02

网友点评