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”,
}]

4.备注

1.创建Json。

/* feeType char 类型*/

cJSON_AddStringToObject(savingAccountFee, “feeType”, feeType);

/* feeAmount int/double 类型 */
cJSON_AddNumberToObject(savingAccountFee, “feeAmount”, feeAmount);

/常用赋值函数/

#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())

#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())

#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())

#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))

#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))

#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))

2.解析Json,不同类型,进行解析时,赋值方式。

/* taxDeduct double 类型 */
pLeaf =cJSON_GetObjectItem(pSub1, "taxDeduct");
if(pLeaf != NULL)
{
printf("---GET:taxDeduct = [name:[%s]_type:[%d]_value:[%d] ]\n", pLeaf->string,pLeaf->type,pLeaf->valuedouble);
p->endorsementResult.taxDeduct = pLeaf->valuedouble;
}
/* totalNum int 类型*/
pLeaf = cJSON_GetObjectItem(pSub1, "totalNum");
if(pLeaf != NULL)
{
p->total.totalNum = pLeaf->valueint;
}
/*companyCode char 类型*/
pLeaf = cJSON_GetObjectItem(pSub1, "companyCode");
if(pLeaf != NULL)
{
strcpy(p->total.companyCode , pLeaf->valuestring);
}