请教armink,s2j库如何对含有结构体数组的序列化等操作

发布于 2018-12-28 09:43:17
例子:
typedef struct
{
idL_Base idL[10];
uint16_t type;
uint16_t rspT;
uint16_t sIntv;
uint16_t dIntv;
uint32_t pId;
uint8_t jsonUp;
}CFG_MAP;


typedef struct
{
uint8_t s;
uint8_t e;
}idL_Base;

像这种内部有其他结构体,不知道s2j库是否支持,若支持,具体用法如何?

谢谢!

查看更多

关注者
0
被浏览
547
3 个回答
sync
sync 2020-03-25
需要自己写对应的转换函数,例子如下
typedef struct {
char name[16];
} Hometown;

typedef struct {
uint8_t id;
double weight;
uint8_t score[8];
char name[10];
Hometown hometown;
} Student;

/**
* Student JSON object to structure object
*
* @param json_obj JSON object
*
* @return structure object
*/
static void *json_to_struct(cJSON* json_obj) {
/* create Student structure object */
s2j_create_struct_obj(struct_student, Student);

/* deserialize data to Student structure object. */
s2j_struct_get_basic_element(struct_student, json_obj, int, id);
s2j_struct_get_array_element(struct_student, json_obj, int, score);
s2j_struct_get_basic_element(struct_student, json_obj, string, name);
s2j_struct_get_basic_element(struct_student, json_obj, double, weight);

/* deserialize data to Student.Hometown structure object. */
s2j_struct_get_struct_element(struct_hometown, struct_student, json_hometown, json_obj, Hometown, hometown);
s2j_struct_get_basic_element(struct_hometown, json_hometown, string, name);

/* return Student structure object pointer */
return struct_student;
}

/**
* Student structure object to JSON object
*
* @param struct_obj structure object
*
* @param JSON object
*/
static cJSON *struct_to_json(void* struct_obj) {
Student *struct_student = (Student *)struct_obj;

/* create Student JSON object */
s2j_create_json_obj(json_student);

/* serialize data to Student JSON object. */
s2j_json_set_basic_element(json_student, struct_student, int, id);
s2j_json_set_basic_element(json_student, struct_student, double, weight);
s2j_json_set_array_element(json_student, struct_student, int, score, 8);
s2j_json_set_basic_element(json_student, struct_student, string, name);

/* serialize data to Student.Hometown JSON object. */
s2j_json_set_struct_element(json_hometown, json_student, struct_hometown, struct_student, Hometown, hometown);
s2j_json_set_basic_element(json_hometown, struct_hometown, string, name);

/* return Student JSON object pointer */
return json_student;
}

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友