这个文章讲一下在NCS上面如何使用JSON库,我使用的是Hello_world例程
1、 宏的配置
CONFIG_LOG=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_CJSON_LIB=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
2、 main文件添加头文件
#include <stdio.h>
#include "cJSON.h"
3、调用库就行了
int main(void)
{
double Heart_Rate = 5.5;
uint8_t buf[15] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x0A,0X0B,0X0C,0X0D,0X0E};
cJSON *root =cJSON_CreateObject();
cJSON *arr = cJSON_AddArrayToObject(root,"test");
cJSON_AddNumberToObject(root,"Heart rate",Heart_Rate);
for ( uint8_t i = 0; i < sizeof(buf); ++i)
{
cJSON_AddItemToArray(arr,cJSON_CreateNumber(buf));
}
char *str=cJSON_Print(root);
printk("%s",cJSON_Print(root));
printf("Hello World! %s\n", CONFIG_BOARD_TARGET);
return 0;
}
4、测试
|