#include #include#include"cJSON.h"
/*Parse text to JSON, then render back to text, and print!*/
void doit(char *text)
{char *out;cJSON *json;
json=cJSON_Parse(text);if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{out=cJSON_Print(json);
cJSON_Delete(json);
printf("%s\n",out);
free(out);
}
}/*Read a file, parse, render back, etc.*/
void dofile(char *filename)
{
FILE*f=fopen(filename,"rb");fseek(f,0,SEEK_END);long len=ftell(f);fseek(f,0,SEEK_SET);char *data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f);
doit(data);
free(data);
}/*Used by some code below as an example datatype.*/
struct record {const char *precision;double lat,lon;const char *address,*city,*state,*zip,*country; };/*Create a bunch of objects as demonstration.*/
voidcreate_objects()
{
cJSON*root,*fmt,*img,*thm,*fld;char *out;int i; /*declare a few.*/
/*Here we construct some JSON standards, from the JSON site.*/
/*Our "Video" datatype:*/root=cJSON_CreateObject();
cJSON_AddItemToObject(root,"name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToOb