将数据序列化为JSON格式,序列化操作完成后务使用Myfree()函数来释放内存
void Josn_format()
{
//cJSON构造方法
cJSON *root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "ADC1", Dev_data.ai1);
cJSON_AddNumberToObject(root, "ADC2", Dev_data.ai2);
cJSON_AddNumberToObject(root, "ADC3", Dev_data.ai3);
cJSON_AddNumberToObject(root,"DI1", Dev_data.input[0]);
cJSON_AddNumberToObject(root,"DI2", Dev_data.input[1]);
cJSON_AddNumberToObject(root,"DI3", Dev_data.input[2]);
cJSON_AddNumberToObject(root,"DI4", Dev_data.input[3]);
cJSON_AddNumberToObject(root,"DI5", Dev_data.input[4]);
cJSON_AddNumberToObject(root,"DI6", Dev_data.input[5]);
cJSON_AddNumberToObject(root,"DI7", Dev_data.input[6]);
cJSON_AddNumberToObject(root,"DI8", Dev_data.input[7]);
cJSON_AddNumberToObject(root,"DI9", Dev_data.input[8]);
cJSON_AddNumberToObject(root,"DI10", Dev_data.input[9]);
cJSON_AddNumberToObject(root,"DI11", Dev_data.input[10]);
cJSON_AddNumberToObject(root,"DI12", Dev_data.input[11]);
cJSON_AddNumberToObject(root,"DI13", Dev_data.input[12]);
cJSON_AddNumberToObject(root,"DI14", Dev_data.input[13]);
cJSON_AddNumberToObject(root,"DI15", Dev_data.input[14]);
cJSON_AddNumberToObject(root,"DI16", Dev_data.input[15]);
char *s = cJSON_PrintUnformatted(root);
int size=strlen(s)+1;
memcpy(Dev_data.json_string,s,size);
cJSON_Delete(root);
myfree(s);
}
1