cJSON仅有一个.h文件和.c文件组成的JSON解析器,源码可网上搜索下载
josn:
- {
- "data": {
- "signCount": 1
- },
- "signList": [
- {
- "fingerPrint": {
- "fpPicBase64": "123"
- },
- "page": 1,
- "signName": {
- "signPicBase64": "base64",
- "signXML": "xml",
- "x": 100,
- "y": 100
- }
- }
- ]
- }
- cJSON * root = cJSON_Parse(text);
- if(!root) {
- printf("cJSON_Parse error\n");
- fflush(stdout);
- return -1;
- }
- do{
- cJSON *data= cJSON_GetObjectItem(root, "data");
- if(nullptr == data || cJSON_Object != data->type){
- printf("data isn't an Object:%d\n",data->type);
- fflush(stdout);
- break;
- }
- cJSON *signList= cJSON_GetObjectItem(root, "signList");
- if(nullptr == signList || cJSON_Array != signList->type){
- printf("signList isn't a array\n");
- fflush(stdout);
- break;
- }
- int array_size = cJSON_GetArraySize(signList);
- if(0 == array_size){
- printf("signList empty\n");
- fflush(stdout);
- break;
- }
- for(int i=0; i< array_size; i++)
- {
- cJSON* item = cJSON_GetArrayItem(signList, i);
- if(nullptr == item || cJSON_Object != item->type){
- printf("item nullptr\n");
- fflush(stdout);
- break;
- }
- cJSON *signName = cJSON_GetObjectItem(item,"signName");
- if(nullptr == signName || cJSON_Object != signName->type){
- printf("signName isn't an Object:%d\n",signName->type);
- fflush(stdout);
- break;
- }
- cJSON *signPicBase64 = cJSON_GetObjectItem(signName,"signPicBase64");
- if(nullptr != signPicBase64 && cJSON_String == signPicBase64->type && signPicBase64->valuestring){
- printf("signPicBase64:%s\n",signPicBase64->valuestring);
- fflush(stdout);
- }
- cJSON *signXML = cJSON_GetObjectItem(signName,"signXML");
- if(nullptr != signXML && cJSON_String == signXML->type && signPicBase64->valuestring){
- printf("signXML:%s\n",signXML->valuestring);
- fflush(stdout);
- }
- }
- }while(0);
- cJSON_Delete(root);
|