- 论坛徽章:
- 15
|
回复 yulihua49
我的难点在于
用C怎么得到一个结构的名称
比如
struct test
{
int a;
ch ...
keytounix 发表于 2011-11-19 16:43 ![]() - vi xx.c
- #include <pack.h>
- #include <json_pack.h>
- T_PkgType xx_tpl[]={
- {CH_INT,sizeof(int),"a",NULL,-1},
- {CH_CLOB,-1,"b"},
- {CH_CLOB,-1,"c"},
- {-1,0,"xx",0}
- };
- typedef struct {
- int a;
- char *b;
- char *c;
- } xx_stu;
- int main()
- {
- xx_stu xx;
- JSON_OBJECT json=json_object_new_object();
- T_PkgType *tp;
- xx.a=123456;
- xx.b="abcd";
- xx.c="ABCD";
- struct_to_json(json,&xx,xx_tpl,NULL);
- printf("xx:%s\n",
- json_object_to_json_string(json));
- json_object_put(json);
- for(tp=xx_tpl;tp->type>-1;tp++)
- printf("%s offset=%d,",tp->name,tp->offset);
- printf("\nsizeof xx=%d\n",tp->offset);
- return 0;
- }
- "xx.c" 35L, 617C 已写入
- tjs@linux:~/src> make xx
- cc -I/workspace/project/ticket-query/02-src/train-lines/sdbc/include -c -o xx.o xx.c
- cc -o xx xx.o -L/workspace/project/ticket-query/02-src/train-lines/sdbc/lib -lstr -ljson -lm -lpthread
- tjs@linux:~/src> ./xx
- xx:{ "a": "123456", "b": "abcd", "c": "ABCD" }
- a offset=0,b offset=4,c offset=8,
- sizeof xx=12
复制代码 |
|