Cjson analysis of a case-pass

Source: Internet
Author: User

#include  <iostream> #include   "Cjson/cjson.h" Using namespace std;void parse_json_ Node ( cJSON * json,size_t * align_count ) {    //  print indent     for (int i = 0;i<*align_count;i++)     {         printf ("  ");    }     switch (Json->type)     {    case cJSON_False:         printf ("%s : false", json->string);         break;    case cJSON_True:         printf ("%s : true", json->string);         break;    case cJSON_NULL:         printf ("null"); &NBSP;&NBSP;&NBSP;&Nbsp;    break;    case cjson_number:         printf ("%s : %ll", json->string,json->valuedouble);         break;    case cJSON_String:         printf ("%s : \"%s\ "", json->string,json->valuestring);         break;    case cJSON_Array:         {            if ( json->string)             {                 printf ("%s : ", json- >string);            }             ++*align_count;            printf ("[\ n");             int len = cjson_ Getarraysize (JSON);             for (int i  = 0;i<len;i++)             {                 cJSON *  Child_json = cjson_getarrayitem (json,i);                 parse_json_node ( child_json , align_count);                 printf ("\ n");             }             --*align_Count;            for (int i = 0;i <*align_count;i++)             {                 printf ("  ");             }             printf ("]");        }         break;    case cJSON_Object:         {            if ( json->string)             {                 printf ("%s : ", json- >string);             }             ++*align_count;             printf ("{\ n");             int len =  cjson_getarraysize (JSON);             for (int  i = 0;i<len;i++)             {                 cjson *  child_json = cjson_getarrayitem (json,i);                 parse_json_node ( child_json , align_count);                 printf ("\ n");             }            --* Align_count;            for (int i =  0;i<*align_count;i++)             {                 printf ("   ");            }              printf ("}");         }         break;    }}int main () {     long long filesize = 0;    file * file =  fopen ("Json/test5", "rb+");     fseek (file,0,seek_end);     Filesize = ftell (file); &Nbsp;   fseek (file,0,seek_set);     char * buff = new  char[filesize];    fread (Buff,1,filesize,file);     fclose (file);     cjson * json = cjson_parse (buff);     size_t  align_count = 0;    parse_json_node (Json,&align_count);    /* cjson * menu = cjson_getobjectitem (JSON, "menu");     int  menu_size = cjson_getarraysize (menu);     cjson * items =  cjson_getarrayitem (menu,1);     int items_size = cjson_getarraysize ( items);     cjson * item1 = cjson_getarrayitem (items,0);     int item1_size = cjson_getarraysize (item1);     cjson *  item1_attr1 = cjSon_getarrayitem (item1,0);     */    return 0;} 

Simple and clear, no need to explain!

This article is from the "Ice Fox Prodigal Blog" blog, please be sure to keep this source http://bhlzlx.blog.51cto.com/3389283/1591089

Cjson analysis of a case-pass

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.