找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1404|回复: 0
收起左侧

CJSON源码 用于解析JSON格式数据

[复制链接]
ID:183545 发表于 2019-5-28 20:45 | 显示全部楼层 |阅读模式
用于解析JSON格式数据

源程序如下:
  1. /*
  2.   Copyright (c) 2009 Dave Gamble

  3.   Permission is hereby granted, free of charge, to any person obtaining a copy
  4.   of this software and associated documentation files (the "Software"), to deal
  5.   in the Software without restriction, including without limitation the rights
  6.   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7.   copies of the Software, and to permit persons to whom the Software is
  8.   furnished to do so, subject to the following conditions:

  9.   The above copyright notice and this permission notice shall be included in
  10.   all copies or substantial portions of the Software.

  11.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17.   THE SOFTWARE.
  18. */

  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "cJSON.h"

  22. /* Parse text to JSON, then render back to text, and print! */
  23. void doit(char *text)
  24. {
  25.         char *out;cJSON *json;
  26.        
  27.         json=cJSON_Parse(text);
  28.         if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}
  29.         else
  30.         {
  31.                 out=cJSON_Print(json);
  32.                 cJSON_Delete(json);
  33.                 printf("%s\n",out);
  34.                 free(out);
  35.         }
  36. }

  37. /* Read a file, parse, render back, etc. */
  38. void dofile(char *filename)
  39. {
  40.         FILE *f=fopen(filename,"rb");fseek(f,0,SEEK_END);long len=ftell(f);fseek(f,0,SEEK_SET);
  41.         char *data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f);
  42.         doit(data);
  43.         free(data);
  44. }

  45. /* Used by some code below as an example datatype. */
  46. struct record {const char *precision;double lat,lon;const char *address,*city,*state,*zip,*country; };

  47. /* Create a bunch of objects as demonstration. */
  48. void create_objects()
  49. {
  50.         cJSON *root,*fmt,*img,*thm,*fld;char *out;int i;        /* declare a few. */

  51.         /* Here we construct some JSON standards, from the JSON site. */
  52.        
  53.         /* Our "Video" datatype: */
  54.         root=cJSON_CreateObject();       
  55.         cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
  56.         cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
  57.         cJSON_AddStringToObject(fmt,"type",                "rect");
  58.         cJSON_AddNumberToObject(fmt,"width",                1920);
  59.         cJSON_AddNumberToObject(fmt,"height",                1080);
  60.         cJSON_AddFalseToObject (fmt,"interlace");
  61.         cJSON_AddNumberToObject(fmt,"frame rate",        24);
  62.        
  63.         out=cJSON_Print(root);        cJSON_Delete(root);        printf("%s\n",out);        free(out);        /* Print to text, Delete the cJSON, print it, release the string. */

  64.         /* Our "days of the week" array: */
  65.         const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  66.         root=cJSON_CreateStringArray(strings,7);

  67.         out=cJSON_Print(root);        cJSON_Delete(root);        printf("%s\n",out);        free(out);

  68.         /* Our matrix: */
  69.         int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}};
  70.         root=cJSON_CreateArray();
  71.         for (i=0;i<3;i++) cJSON_AddItemToArray(root,cJSON_CreateIntArray(numbers[i],3));

  72. /*        cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */
  73.        
  74.         out=cJSON_Print(root);        cJSON_Delete(root);        printf("%s\n",out);        free(out);


  75.         /* Our "gallery" item: */
  76.         int ids[4]={116,943,234,38793};
  77.         root=cJSON_CreateObject();
  78.         cJSON_AddItemToObject(root, "Image", img=cJSON_CreateObject());
  79.         cJSON_AddNumberToObject(img,"Width",800);
  80.         cJSON_AddNumberToObject(img,"Height",600);
  81.         cJSON_AddStringToObject(img,"Title","View from 15th Floor");
  82.         cJSON_AddItemToObject(img, "Thumbnail", thm=cJSON_CreateObject());
  83.         cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
  84.         cJSON_AddNumberToObject(thm,"Height",125);
  85.         cJSON_AddStringToObject(thm,"Width","100");
  86.         cJSON_AddItemToObject(img,"IDs", cJSON_CreateIntArray(ids,4));

  87.         out=cJSON_Print(root);        cJSON_Delete(root);        printf("%s\n",out);        free(out);

  88.         /* Our array of "records": */
  89.         struct record fields[2]={
  90.                 {"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"},
  91.                 {"zip",37.371991,-1.22026e+2,"","SUNNYVALE","CA","94085","US"}};

  92.         root=cJSON_CreateArray();
  93.         for (i=0;i<2;i++)
  94.         {
  95.                 cJSON_AddItemToArray(root,fld=cJSON_CreateObject());
  96.                 cJSON_AddStringToObject(fld, "precision", fields[i].precision);
  97.                 cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat);
  98.                 cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon);
  99.                 cJSON_AddStringToObject(fld, "Address", fields[i].address);
  100.                 cJSON_AddStringToObject(fld, "City", fields[i].city);
  101.                 cJSON_AddStringToObject(fld, "State", fields[i].state);
  102.                 cJSON_AddStringToObject(fld, "Zip", fields[i].zip);
  103.                 cJSON_AddStringToObject(fld, "Country", fields[i].country);
  104.         }
  105.        
  106. /*        cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4)); */
  107.        
  108.         out=cJSON_Print(root);        cJSON_Delete(root);        printf("%s\n",out);        free(out);

  109. }

  110. int main (int argc, const char * argv[]) {
  111.         /* a bunch of json: */
  112.         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}";       
  113.         char text2[]="[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]";
  114.         char text3[]="[\n    [0, -1, 0],\n    [1, 0, 0],\n    [0, 0, 1]\n        ]\n";
  115.         char text4[]="{\n                \"Image\": {\n                        \"Width\":  800,\n                        \"Height\": 600,\n                        \"Title\":  \"View from 15th Floor\",\n                        \"Thumbnail\": {\n                                \"Url\":    \"http:/*www.example.com/image/481989943\",\n                                \"Height\": 125,\n                                \"Width\":  \"100\"\n                        },\n                        \"IDs\": [116, 943, 234, 38793]\n                }\n        }";
  116.         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         ]";

  117.         /* Process each json textblock by parsing, then rebuilding: */
  118.         doit(text1);
  119.         doit(text2);       
  120.         doit(text3);
  121.         doit(text4);
  122.         doit(text5);

  123.         /* Parse standard testfiles: */
  124. /*        dofile("../../tests/test1"); */
  125. /*        dofile("../../tests/test2"); */
  126. /*        dofile("../../tests/test3"); */
  127. /*        dofile("../../tests/test4"); */
  128. /*        dofile("../../tests/test5"); */

  129.         /* Now some samplecode for building objects concisely: */
  130.         create_objects();
  131.        
  132.         return 0;
  133. }

复制代码

所有资料51hei提供下载:
cJSON.zip (19.07 KB, 下载次数: 41)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表