你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F103 cJson 嵌套解析,求指导

[复制链接]
wdshuang09 提问时间:2019-3-21 22:06 /
上位机发送json,{"cmd" : "device_hard","desc" : "dyaw","out":{"desc1":"V1.00","type":"string"}}STM32f1串口接收到数组里,"out":{"desc":"V1.00","type":"string"}不知道怎么解析出来,求指导。
        cJSON *root;
        cJSON *cmd;
        cJSON *desc;
        cJSON *output;
        cJSON *hard;
        cJSON *taskArry;
        cJSON *desc1;
        cJSON *type;
        cJSON * temp;
        int i ,icount;

        root = mymalloc(SRAMIN,sizeof(cJSON));
        root = cJSON_Parse((const char*)uart1Buf.Rx_Buffer);

       cmd= cJSON_GetObjectItem(root,"cmd");
       if(desc != NULL)
        {

         printf("cmd= %s\r\n",(cmd->valuestring));
       }
        desc= cJSON_GetObjectItem(root,"desc");
        if(desc != NULL)
        {
                printf("desc= %s\r\n",(desc->valuestring));
        }
        //以下这段解析不对,不知道怎么修改了
        temp=cJSON_GetObjectItem(root,"output");
        if(temp != NULL)
        {
                icount = cJSON_GetArraySize(output);
                printf( "icount=%d : \n",icount);
                for(i=0; i < icount; i++)
                {
                         output=cJSON_GetArrayItem(output, i);
                        if(output != NULL)
                        {
                                desc1 = cJSON_GetObjectItem(output, "desc1");
                                printf("desc1= %s\r\n",(desc1->valuestring));
                                type= cJSON_GetObjectItem(output, "type");
                                printf("type= %s\r\n",(type->valuestring));
                        }
                       
                }

   }
cJSON_Delete(root);       
my_free(root);
my_free(cmd);

<
收藏 评论18 发布时间:2019-3-21 22:06

举报

18个回答
yklstudent-1794 回答时间:2019-3-22 09:05:35
"out":{"desc":"V1.00","type":"string"}
这个数据内容有问题,应该为:"out":[{"desc":"V1.00","type":"string"}]

评分

参与人数 1蝴蝶豆 +2 收起 理由
STMCU + 2

查看全部评分

mj1114 回答时间:2019-3-22 14:21:56
where is "output"
wdshuang09 回答时间:2019-3-22 20:53:54
yklstudent-1794 发表于 2019-3-22 09:05
"out":{"desc":"V1.00","type":"string"}
这个数据内容有问题,应该为:"out":[{"desc":"V1.00","type":"st ...

"out":[{"desc":"V1.00","type":"string"}]如果是这样,应该怎么解析?
yklstudent-1794 回答时间:2019-3-22 21:38:38
wdshuang09 发表于 2019-3-22 20:53
"out":[{"desc":"V1.00","type":"string"}]如果是这样,应该怎么解析?

const char * m_json_context =
{
        "{\"cmd\" : \"device_hard\",\"desc\" : \"dyaw\",\"out\":[{\"desc1\":\"V1.00\",\"type\":\"string\"}]}"
};

const cJSON *json_body = NULL;
const cJSON *json_list = NULL;
const cJSON *m_id_str = NULL;
const cJSON *m_price_str = NULL;

bool json_parse_info(const char * const context)
{
        bool status = false;
       
        cJSON *json_context = cJSON_Parse(context);
       
        if(json_context == NULL)
        {
                const char *err_ptr = cJSON_GetErrorPtr();
                if(err_ptr != NULL)
                {
                        usart_send_data_packet(&usart_drivers[eUSART1], (uint8_t*)err_ptr, strlen(err_ptr));
                }
                cJSON_Delete(json_context);
                return status;
        }
        // 项目01
        json_body = cJSON_GetObjectItemCaseSensitive(json_context, "cmd");
        if( !cJSON_IsString(json_body) )
        {
                cJSON_Delete(json_context);
                return status;
        }
        // 项目02
        json_body = cJSON_GetObjectItemCaseSensitive(json_context, "desc");
        if( !cJSON_IsString(json_body) )
        {
                cJSON_Delete(json_context);
                return status;
        }
        // 项目03
        json_body = cJSON_GetObjectItemCaseSensitive(json_context, "out");
        cJSON_ArrayForEach(json_list, json_body)
        {
                // xx1
                m_id_str = cJSON_GetObjectItemCaseSensitive(json_list, "desc1");
                // xx2
                m_price_str = cJSON_GetObjectItemCaseSensitive(json_list, "type");
                // 判断处理
                if( !cJSON_IsString(m_id_str) || !cJSON_IsString(m_price_str) )
                {
                        cJSON_Delete(json_context);
                        return status;
                }
        }
        cJSON_Delete(json_context);
        return status;
}
wdshuang09 回答时间:2019-3-23 13:09:10
yklstudent-1794 发表于 2019-3-22 21:38
const char * m_json_context =
{
        "{\"cmd\" : \"device_hard\",\"desc\" : \"dyaw\",\"out\":[{\"desc ...

cJSON_GetObjectItemCaseSensitive()这个函数在CJson没有找到,后面网上找到了cJSON-master这个里面有这个函数,再弱弱的想请教一下,用cJSON-master的库怎么移植到STM32F103中?我没有移植成功,能否提供一个例子参考一下谢谢!
我的cjson移植是参考http://www.openedv.com/forum.php?mod=viewthread&tid=229818
yklstudent-1794 回答时间:2019-3-23 19:11:16
wdshuang09 发表于 2019-3-23 13:09
cJSON_GetObjectItemCaseSensitive()这个函数在CJson没有找到,后面网上找到了cJSON-master这个里面有 ...

你用的老版本?
那就用cJSON_GetObjectItem函数好了
wdshuang09 回答时间:2019-3-24 17:42:40
本帖最后由 wdshuang09 于 2019-3-24 19:46 编辑
yklstudent-1794 发表于 2019-3-23 19:11
你用的老版本?
那就用cJSON_GetObjectItem函数好了

cJSON-master已成功移植STM32F1,现在已解析数据出来,但是出了一个新的问题;原本创建的JSON数据包在老版本JSON库是可以正常发出去的图1,但改成cJSON-master的文件就发送不出去见图片2,不知道问题出在哪?
发送JSON数据包函数如下:
       cJSON *root = NULL;
        char *string = NULL;
        root= cJSON_CreateObject();
        cJSON_AddItemToObject(root, "cmd", cJSON_CreateString("device_desc"));
        cJSON_AddItemToObject(root, "desc", cJSON_CreateString("dzfz")) ;
        cJSON_AddItemToObject(root, "id", cJSON_CreateString("dzfzno1"));
        cJSON_AddItemToObject(root, "hard", cJSON_CreateString("V1.00"));
        cJSON_AddItemToObject(root, "row", cJSON_CreateString("4"));
        __nop();
        string = cJSON_Print(root);
        __nop();
        printf("%s\n",string);
        if (string == NULL)
        {
                printf("\nFailed to print root.\n");
        }
        my_free(string);
        cJSON_Delete(root);
        my_free(root);

图2

图2

图1

图1
yklstudent-1794 回答时间:2019-3-24 20:03:34
wdshuang09 发表于 2019-3-24 17:42
cJSON-master已成功移植STM32F1,现在已解析数据出来,但是出了一个新的问题;原本创建的JSON数据包在老版 ...

上工程看看
wdshuang09 回答时间:2019-3-24 21:18:20
本帖最后由 wdshuang09 于 2019-4-12 20:38 编辑

工程文件已上传,请帮忙看一下
12下一页

所属标签

相似问题

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版