C语言调用结构体注意点

//请求JSON
typedef struct{
char *function;
char *params;
char *sid;
//终端的APPkey(最后一次修改笔录的终端)="9999999999"
char *app_key;
//timestamp | String | 是 | 时间戳,格式为yyyyMMddHHmmss, 时区为GMT + 8, 平台允许客户端请求最大时间误差为60分钟。 |
char *timestamp;
}RequestData;
//用户登录参数
typedef struct{
char *loginId;
char *password;
char *loginFrom;
}LoginUser;

//获取用户信息
LoginUser getLoginUser(char *loginId,char *password,char *loginFrom){
LoginUser luser;
luser.loginId=loginId;
luser.password=password;
luser.loginFrom=loginFrom;
return luser;
}
//获取登录用户JSON
char * getLoginUserJson(LoginUser lu){
char json[1000];
sprintf(json,"%s%s%s%s%s%s%s","\"loginId\":\"",lu.loginId,"\",\"loginId\":\"",lu.password,"\",\"loginFrom\":\"",lu.loginFrom,"\"");
return json;
}
//获取请求参数信息
RequestData getRequestData(char *function,char *params){
RequestData rdata;
rdata.function=function;
rdata.params=params;
rdata.sid="1ef9cf08-9181-4886-a19d-908774645515";
rdata.app_key="9999999999";
rdata.timestamp="20190924222359";
return rdata;
}
//获取登录用户请求参数JSON
char * getRequestDataJson(RequestData rdata){
char datajson[1000];
//lr_message("5555555555%s",rdata.app_key);
//sprintf(datajson,"%s%s%s%s%s%s%s%s%s%s%s","\"function\":\"",rdata.function,"\",\"params\":\"{",rdata.params,"},\"sid\":\"",rdata.sid,"\",\"app_key\":\"",rdata.app_key,"\",\"timestamp\":\"",rdata.timestamp,"\"");
sprintf(datajson,"%s%s%s%s%s%s%s%s%s%s","body=function=",rdata.function,"&params={",rdata.params,"}&sid=",rdata.sid,"&app_key=",rdata.app_key,"&timestamp=",rdata.timestamp);

//lr_message("77777777%s",datajson);
return datajson;
}
//初始化请求参数的值,并返回实际的请求参数
// char * requestLoginjson(){
// LoginUser lu=getLoginUser("admin","123456","1");
// char *luserJson=getLoginUserJson(lu);
// RequestData rdata;
// char * rdataJson;
// char *json=NULL;
// lr_message("%s",luserJson);
// lr_save_string(luserJson,"userJson");
// rdata=getRequestData("loginPwd",lr_eval_string("{userJson}"));
// rdataJson=getRequestDataJson(rdata);
// lr_message("%s",rdataJson);
// lr_save_string(rdataJson,"dataJson");
// lr_message("333333%s",lr_eval_string("{dataJson}"));
// json = (char *)malloc(sizeof(char)*strlen(lr_eval_string("{dataJson}")));
// strcpy(json, lr_eval_string("{dataJson}"));
// //lr_message("%s22222",strlen(lr_eval_string("{dataJson}")));
// lr_message("888888%s",json);
//
// return json;
// }


/*
写成过程方法,无返回值,保存值到参数,主函数通过参数打印
*/
void requestLoginjson(){
LoginUser lu=getLoginUser("admin","123456","1");
char *luserJson=getLoginUserJson(lu);
RequestData rdata;
char * rdataJson;
char *json=NULL;
lr_message("%s",luserJson);
lr_save_string(luserJson,"userJson");
rdata=getRequestData("loginPwd",lr_eval_string("{userJson}"));
rdataJson=getRequestDataJson(rdata);
lr_message("%s",rdataJson);
lr_save_string(rdataJson,"dataJson");
lr_message("333333%s",lr_eval_string("{dataJson}"));
json = (char *)malloc(sizeof(char)*strlen(lr_eval_string("{dataJson}")));
strcpy(json, lr_eval_string("{dataJson}"));
//lr_message("%s22222",strlen(lr_eval_string("{dataJson}")));
lr_message("888888%s",json);
lr_save_string(json,"json_Par");
}

Action()
{
// char *jsonstr=requestLoginjson();
// lr_message("11111111111%s",jsonstr);
// free(jsonstr);
requestLoginjson();
lr_output_message(lr_eval_string("{json_Par}"));
return 0;
}

posted on 2021-12-07 11:31  seamy  阅读(125)  评论(0编辑  收藏  举报