没有合适的资源?快使用搜索试试~ 我知道了~
首页STM32学习笔记之Cjson的使用
解析 需要用到的几个函数: 1、解析JSONJ结构得到cjson对象:cJSON * root=cJSON_Parse(char *buf); 2、获取无格式的json对象:cJSON_PrintUnformatted(cJSON *item) 3、根据键值获取对应的值:cJSON *cJSON_GetObjectItem(cJSON *object,const char *string); 假设一串JSON字符串如下: {“Address”:“111D6FFFFE12459D”,“CommandType”:“010D”,“EndpointId”:“1”,“Command”:{“Operate
资源详情
资源评论
资源推荐

STM32学习笔记之学习笔记之Cjson的使用的使用
解析解析
需要用到的几个函数:
1、解析JSONJ结构得到cjson对象:cJSON * root=cJSON_Parse(char *buf);
2、获取无格式的json对象:cJSON_PrintUnformatted(cJSON *item)
3、根据键值获取对应的值:cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
假设一串JSON字符串如下:
{“Address”:“111D6FFFFE12459D”,“CommandType”:“010D”,“EndpointId”:“1”,“Command”:{“Operate”:“0”}}
解析如下:
cjson_command com;//此结构体为接收json各个键值的数据
cJSON * root=cJSON_Parse(char *buf);//buf指向cjson字符串数据
c_add =cJSON_GetObjectItem(root,“Address”);
strcpy(com.Address,c_add->valuestring);
cJSON * c_ctype =NULL;
c_ctype =cJSON_GetObjectItem(root,“CommandType”);
strcpy(com.CommandType,c_ctype->valuestring);
cJSON * c_Epid =NULL;
c_Epid =cJSON_GetObjectItem(root,“EndpointId”);
strcpy(com.Endpointid,c_Epid->valuestring);
cJSON * c_Com =NULL;
c_Com =cJSON_GetObjectItem(root,“Command”);
if(c_Com == NULL)
{
u3_printf(“no command”);
}else{
cJSON * c_Ope =NULL;
c_Ope =cJSON_GetObjectItem(c_Com,“Operate”);
if(c_Ope == NULL)
{
u3_printf(“no Operate!”);
}else
{
strcpy(com.Operate,c_Ope->valuestring);
u3_printf(“Operate:%s”,com.Operate);
}
cJSON_Delete(root);
注意:注意:
我的例子里 json字符串的值全为char类型的,可根据实际情况设置变量,例如c_ctype->valuestring可以为valueint、
valuedouble。
合成合成
合成结果如下:
{
“Address”: “1234567890ABCDEF”,
“EndpointId”: “1”,




















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0