请选择 进入手机版 | 继续访问电脑版
查看: 2090|回复: 1

MK3060基于Alios-Things的cjson用法

[复制链接]

7

主题

16

帖子

194

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
194
发表于 2018-9-12 09:33:03 | 显示全部楼层 |阅读模式
本帖最后由 lizhibo32 于 2018-9-12 09:35 编辑

1.我们使用wifi模块进行Alios-Things系统的二次开发工作,几乎必须和json打交道,接下来我们便使用Alios-Things自带的cjson,进行解析一个比较复杂的json串,开发板使用庆科MK3060。

2.待解析的json串如下所示:
{
        "sequence": "12345",
        "deviceId": "XXX",
        "serialId": "0",
        "cmd": [{
                "cmdName": "SET_POWER",
                "cmdParam": "10"
        }, {
                "cmdName": "SET_MODE",
                "cmdParam": "2"
        }],
        "cmd2": {
                "cmdName": "SET_POWER",
                "cmdParam": "10"
        },
        "time": 1361542433
}

3.参考eample路径下的helloworld工程,新建一个名为cjson的新的工程,工程目录结构如下:
    -cjson:
              --cjson_main.c
              --cjson.mk
    其中cjson_main.c内容如下所示:

  1. /**
  2. ******************************************************************************
  3. * @file    hello_world.c
  4. * @author  William Xu
  5. * @version V1.0.0
  6. * @date    21-May-2015
  7. * @brief   First MiCO application to say hello world!
  8. ******************************************************************************
  9. *
  10. *  The MIT License
  11. *  Copyright (c) 2016 MXCHIP Inc.
  12. *
  13. *  Permission is hereby granted, free of charge, to any person obtaining a copy
  14. *  of this software and associated documentation files (the "Software"), to deal
  15. *  in the Software without restriction, including without limitation the rights
  16. *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. *  copies of the Software, and to permit persons to whom the Software is furnished
  18. *  to do so, subject to the following conditions:
  19. *
  20. *  The above copyright notice and this permission notice shall be included in
  21. *  all copies or substantial portions of the Software.
  22. *
  23. *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  27. *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  28. *  IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. *
  30. ******************************************************************************
  31. */

  32. #include <aos/aos.h>
  33. #include "cJSON.h"

  34. #define LOG_TAG "json_test"
  35. #define app_debug_log(format, ...) LOGI(LOG_TAG, format"\r\n",##__VA_ARGS__)
  36. #define app_info_log(format, ...) LOGI(LOG_TAG, format"\r\n",##__VA_ARGS__)
  37. #define app_error_log(format, ...) LOGE(LOG_TAG, format"\r\n",##__VA_ARGS__)


  38. char *msg = "{"sequence":"12345","deviceId":"XXX","serialId":"0","cmd":[{"cmdName": "SET_POWER","cmdParam":"10"},{"cmdName":"SET_MODE","cmdParam":"2"}],"cmd2":{"cmdName": "SET_POWER","cmdParam":"10"}, "time":1361542433}";

  39. typedef struct _PARA_RESULT_S{
  40.     char para_code[128];
  41.     char para_value[5];
  42. }PARA_RESULT_S;

  43. int get_para_from_buf(PARA_RESULT_S *para, int nums, char *buf, int len)
  44. {
  45.     int ret = 0;
  46.     int num_elements = 0, i;
  47.     cJSON *msg = NULL, *msg_child1 = NULL, *msg_child2 = NULL;

  48.     app_info_log("json_str:%s", buf);
  49.     // Initialise the JSON parser and parse the given string
  50.     cJSON *root = cJSON_Parse(buf);
  51.     if (!root) {
  52.         const char * err = cJSON_GetErrorPtr();
  53.         app_info_log("Error before: [%s]\n",err);
  54.         goto exit;
  55.     } else {
  56.         // char *info = cJSON_Print(root);
  57.         // app_info_log("root is %s", info);
  58.         // free(info);

  59.         cJSON *message =  cJSON_GetObjectItem(root, "sequence");
  60.         if (NULL == message)
  61.         {
  62.             app_info_log("invalid json doc of sequence ");
  63.             goto exit;
  64.         }
  65.         //check whether is positive message
  66.         app_info_log("sequence: %s",message->valuestring);

  67.         message =  cJSON_GetObjectItem(root, "deviceId");
  68.         if (NULL == message)
  69.         {
  70.             app_info_log("invalid json doc of deviceId ");
  71.             goto exit;
  72.         }
  73.         //check whether is positive message
  74.         app_info_log("deviceId: %s",message->valuestring);

  75.         message =  cJSON_GetObjectItem(root, "serialId");
  76.         if (NULL == message)
  77.         {
  78.             app_info_log("invalid json doc of serialId ");
  79.             goto exit;
  80.         }
  81.         //check whether is positive message
  82.         app_info_log("serialId: %s",message->valuestring);

  83.         message =  cJSON_GetObjectItem(root, "time");
  84.         if (NULL == message)
  85.         {
  86.             app_info_log("invalid json doc of time ");
  87.             goto exit;
  88.         }
  89.         //check whether is positive message
  90.         app_info_log("time: %d", message->valueint);

  91.         /*  get cmd:  "cmd":[{"cmdName": "SET_POWER","cmdParam":"10"},{"cmdName":"SET_MODE","cmdParam":"2"}] */
  92.         message =  cJSON_GetObjectItem(root, "cmd");
  93.         num_elements = cJSON_GetArraySize(message);

  94.         app_info_log("nums: %d", num_elements);

  95.         for ( i = 0; i < num_elements; i++ )
  96.         {
  97.             msg = cJSON_GetArrayItem(message, i);

  98.             msg_child1 = cJSON_GetObjectItem( msg, "cmdName");
  99.             strncpy( para[i].para_code, msg_child1->valuestring, sizeof(para[i].para_code) );
  100.             msg_child2 = cJSON_GetObjectItem( msg, "cmdParam");
  101.             strncpy( para[i].para_value, msg_child2->valuestring, sizeof(para[i].para_value) );

  102.             app_info_log("i is %d:    paramCode: %s, paramValue: %s", i, para[i].para_code, para[i].para_value);
  103.         }

  104.         /*  get cmd:  "cmd2":{"cmdName": "SET_POWER","cmdParam":"10"} */
  105.         message =  cJSON_GetObjectItem(root, "cmd2");
  106.         msg_child1 = cJSON_GetObjectItem(message, "cmdName");
  107.         msg_child2 = cJSON_GetObjectItem(message, "cmdParam");
  108.         app_info_log(" paramCode: %s, paramValue: %s", msg_child1->valuestring, msg_child2->valuestring);
  109.     }

  110. exit:
  111.     if (root)
  112.     {
  113.         cJSON_Delete(root);
  114.     }
  115.     return ret;
  116. }

  117. int application_start(void)
  118. {
  119.     int ret = 0;

  120.     PARA_RESULT_S para[2] = { 0x00 };

  121.     app_info_log("application start!");

  122.     get_para_from_buf(para, 2, msg, strlen(msg));

  123.     while ( 1 )
  124.     {
  125.         // app_info_log("cjson test ......");
  126.         aos_msleep(1000);
  127.     }

  128.     return ret;
  129. }

复制代码
   cjson.mk内容如下:
  1. ###############################################################################
  2. #
  3. #  The MIT License
  4. #  Copyright (c) 2016 MXCHIP Inc.
  5. #
  6. #  Permission is hereby granted, free of charge, to any person obtaining a copy
  7. #  of this software and associated documentation files (the "Software"), to deal
  8. #  in the Software without restriction, including without limitation the rights
  9. #  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. #  copies of the Software, and to permit persons to whom the Software is furnished
  11. #  to do so, subject to the following conditions:
  12. #
  13. #  The above copyright notice and this permission notice shall be included in
  14. #  all copies or substantial portions of the Software.
  15. #
  16. #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. #  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. #  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. #  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  21. #  IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. ###############################################################################


  23. NAME := App_cjson

  24. $(NAME)_SOURCES := cjson_main.c

  25. $(NAME)_COMPONENTS := cjson  \
  26.                                           yloop  \
  27.                                           cli    \
  28.                                           log    \
  29.                                           netmgr

复制代码

编译运行结果如下:







本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

4

主题

31

帖子

390

积分

中级会员

Rank: 3Rank: 3

积分
390
发表于 2018-9-13 15:58:38 | 显示全部楼层
学习一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表