cJSON解析一例通

來源:互聯網
上載者:User

標籤:cjson 解析

#include <iostream>#include "cJson/cJSON.h"using namespace std;void parse_json_node( cJSON * json,size_t * align_count ){    // 列印縮排    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");        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);    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;}

簡單明了,無需解釋!

本文出自 “冰狐浪子的部落格” 部落格,請務必保留此出處http://bhlzlx.blog.51cto.com/3389283/1591089

cJSON解析一例通

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.