1.生成 json
char* CreatJsCustNo( BT_BNF_CUST *p, char *strPaperNO) { cJSON *pJsonArry,*pJsonsub; pJsonArry=cJSON_CreateArray(); /*创建数组*/ cJSON_AddItemToArray(pJsonArry,pJsonsub=cJSON_CreateObject()); /* 给创建的数组增加对对象*/ cJSON_AddStringToObject(pJsonsub, "PROV_BRANCH_NO","000002"); /* 给对象增加内容 */ cJSON_AddStringToObject(pJsonsub, "SRC_SYS","UBPS"); cJSON_AddStringToObject(pJsonsub, "CUST_OAC_BRANCH_NO","120101"); cJSON_AddStringToObject(pJsonsub, "ROLE","3"); cJSON_AddStringToObject(pJsonsub, "IPSN_NO","1"); char * pp = cJSON_Print(pJsonArry); if(NULL == pp) { cJSON_Delete(pJsonArry); return NULL; } printf("bbbb\n"); cJSON_Delete(pJsonArry); return pp; }
2.解析
int ParseJsCustNo (char *strJson ,BT_BNF_CUST_RESP *p) { cJSON * pJson,*pSub,*pSub1 ; int iCount=0; if(NULL == strJson) { return -1; } pJson = cJSON_Parse(strJson); /* 解析 json 放入 pJson*/ if(NULL == pJson) { return -1; } printf("传入=%s\n",cJSON_Print(pJson)); iCount = cJSON_GetArraySize(pJson); /*获取数组长度*/ printf("iCount=[%d]\n",iCount); pSub = cJSON_GetArrayItem(pJson,0); /*目前按1笔处理,取出一笔放入 pSub */ printf("数组里面的内容=%s\n",cJSON_Print(pSub)); pSub1 = cJSON_GetObjectItem(pSub, "PARTY_ID"); if(pSub1 != NULL) { printf("---GET:PARTY_ID = [name:[%s]_type:[%d]_value:[%s] ]\n", pSub1->string,pSub1->type,pSub1->valuestring); strcpy(p->PARTY_ID,pSub1->valuestring ); } pSub1 = cJSON_GetObjectItem(pSub, "CUST_NO"); if(pSub1 != NULL) { printf("---GET:CUST_NO = [name:[%s]_type:[%d]_value:[%s] ]\n", pSub1->string,pSub1->type,pSub1->valuestring); strcpy(p->CUST_NO,pSub1->valuestring ); } pSub1 = cJSON_GetObjectItem(pSub, "PROV_BRANCH_NO"); if(pSub1 != NULL) { printf("---GET:PROV_BRANCH_NO = [name:[%s]_type:[%d]_value:[%s] ]\n", pSub1->string,pSub1->type,pSub1->valuestring); strcpy(p->PROV_BRANCH_NO,pSub1->valuestring ); } cJSON_Delete(pJson); return 0; }
结果类似如下:
生成:
[{
"PROV_BRANCH_NO": "000002",
"SRC_SYS": "UBPS",
}]
解析:
[{
"RelCode": "已有客户!",
"PROV_BRANCH_NO": "000002",
"SRC_SYS": "UBPS",
}]
时间: 2024-11-09 03:20:08