### 单片机使用JSON序列化及反序列化使用指南
1. 安装json.pack文件
2. 在keil库管理中选择Data Exchange > JSON > jansson
3. 在使用的地方引入 #include "jansson.h"
###### 构建:普通的JSON键值对
```c
"age": 20,
"score": 78.4,
"name": "whik1194",
"blog": "https://blog.csdn.net/whik1194",
"wechat": "mcu149"
}
//构建函数
#include "jansson.h"
uint8_t Jansson1_Demo(void)
{
json_t *root;
json_t *blog_string;
char *str;
root = json_object();
json_object_set_new(root, "status", json_boolean(JSON_TRUE));
json_object_set_new(root, "age", json_integer(20));
json_object_set_new(root, "score", json_real(78.4));
json_object_set_new(root, "name", json_string("whik1194"));
blog_string = json_string("https://blog.csdn.net/whik1194");
json_object_set_new(root, "blog", blog_string);
json_object_set_new(root, "wechat", json_string("mcu149"));
str = json_dumps(root, JSON_PRESERVE_ORDER);
json_decref(root);
LOG1("\r\njson_string:\r\n%s\r\n", str);
return 0;
}
```
###### 构建:套JSON对象
```C
{
"success": "1",
"result": {
"timestamp": "1592640249",
"datetime_1": "2020-06-20 16:04:09",
"week_1": 6,
"week_2": "Saturday"
}
}
//构建函数
uint8_t Jansson2_Demo(void)
{
json_t *root;
json_t *result_obj;
char *str;
root = json_object();
result_obj = json_object();
json_object_set_new(result_obj, "timestamp", json_string("1592640249"));
json_object_set_new(result_obj, "datetime_1", json_string("2020-06-20 16:04:09"));
json_object_set_new(result_obj, "week_1", json_integer(1));
json_object_set_new(result_obj, "week_2", json_string("Saturday"));
json_object_set_new(root, "success", json_string("1"));
json_object_set_new(root, "result", result_obj);
str = json_dumps(root, JSON_PRESERVE_ORDER);
json_decref(root);
LOG1("\r\njson_string:\r\n%s\r\n", str);
return 0;
}
```
###### 构建:包含数组的JSON对象
```C
{
"location": [{
"name": "Faye",
"address": "Beijing"
}, {
"name": "Andy",
"address": "ShangHai"
}, {
"name": "Lisa",
"address": "ShenZhen"
}],
"time": "2018-11-17"
}
//构建函数
uint8_t Jansson3_Demo(void)
{
json_t *root;
json_t *array_obj;
char *str;
char name[3][20] = {"Faye", "Andy", "Lisa"};
char address[3][20] = {"Beijing", "ShangHai", "ShenZhen"};
uint8_t idx = 0;
json_t *array_value;
root = json_object();
array_obj = json_array();
array_value = json_object();
for(idx = 0; idx < 3; idx++)
{
json_object_set_new(array_value, name[idx], json_string(address[idx]));
json_array_insert_new(array_obj, idx, array_value);
// json_array_append_new(array_obj, array_value);
}
json_object_set_new(root, "location", array_obj); //location键的值是一个数组
json_object_set_new(root, "time", json_string("2018-11-17"));
str = json_dumps(root, JSON_PRESERVE_ORDER);
json_decref(root);
LOG1("\r\njson_string:\r\n%s\r\n", str);
return 0;
}
```
###### 解析:JSON字符串,包括字符串、整形和布尔类型三种类型的键值对
```C
//JSON字符串,包括字符串、整形和布尔类型三种类型的键值对:
{
"status": true,
"age": 20,
"name": "whik1194",
"blog": "https://blog.csdn.net/whik1194",
"wechat": "mcu149"
}
//解析函数
uint8_t Jansson1_Demo(void)
{
const char *text = "{\"status\":true,\"age\":20,\"score\":78.4,\"name\":\"whik1194\",\"blog\":\"https://blog.csdn.net/whik1194\",\"wechat\":\"mcu149\"}";
json_error_t error;
json_t *root;
json_t *status;
char *name, *blog, *wechat;
int age;
double score;
root = json_loads((const char*)text, 0, &error);
if(json_is_object(root))
{
LOG1("root format correct\r\n");
/* string 类型的解析 */
name = (char *)json_string_value(json_object_get(root, "name"));
blog = (char *)json_string_value(json_object_get(root, "blog"));
wechat = (char *)json_string_value(json_object_get(root, "wechat"));
LOG1("name: %s\r\n", name);
LOG1("blog: %s\r\n", blog);
LOG1("wechat: %s\r\n", wechat);
/* int 类型的解析 */
age = json_integer_value(json_object_get(root, "age"));
LOG1("age: %d\r\n", age);
/* double 类型的解析 */
score = json_real_value(json_object_get(root, "score"));
LOG1("score: %.1f\r\n", score);
/* bool 类型的解析 */
status = json_object_get(root, "status");
if(json_is_true(status))
LOG1("status: true\r\n");
else if(json_is_false(status))
LOG1("status: false\r\n");
}
else
{
printf("root format error:%d-%s\r\n", error.line, error.text);
return 1;
}
return 0;
}
```
###### 解析:键的值是一个对象:JSON字符串,其中result键的值是一个JSON对象,包括几组键值对:
```c
{
"success": "1",
"result": {
"timestamp": "1592640249",
"datetime_1": "2020-06-20 16:04:09",
"week_1": "6",
"week_2": "Saturday"
}
}
//解析函数
uint8_t Jansson2_Demo(void)
{
const char *text = "{\"success\":\"1\",\"result\":{\"timestamp\":\"1592640249\",\"datetime_1\":\"2020-06-20 16:04:09\",\"week_1\":\"6\",\"week_2\":\"Saturday\"}}";
json_error_t error;
json_t *root;
json_t *result;
char *success;
char *timestamp;
char *datetime_1;
char *week_1;
root = json_loads((const char*)text, 0, &error);
if(json_is_object(root))
{
LOG1("root format correct\r\n");
success = (char *)json_string_value(json_object_get(root, "success"));
LOG1("success:%s\r\n", success);
result = json_object_get(root, "result");
if(json_is_object(result))
{
timestamp = (char *)json_string_value(json_object_get(result, "timestamp"));
datetime_1 = (char *)json_string_value(json_object_get(result, "datetime_1"));
week_1 = (char *)json_string_value(json_object_get(result, "week_1"));
LOG1("timestamp:%s\r\n", timestamp);
LOG1("datetime_1:%s\r\n", datetime_1);
LOG1("week_1:%s\r\n", week_1);
}
else
{
LOG1("reuslt format error\r\n");
return 1;
}
}
else
{
LOG1("root format error:%d-%s\r\n", error.line, error.text);
return 1;
}
return 0;
}
```
###### 解析:键的值是一个数组
```c
{
"location": [{
"name": "Faye",
"address": "Beijing"
}, {
"name": "Andy",
"address": "ShangHai"
}, {
"name": "Lisa",
"address": "ShenZhen"
}],
"time": "2018-11-17"
}
//解析函数
uint8_t Jansson3_Demo(void)
{
const char *text = "{\"location\":[{\"name\":\"Faye\",\"address\":\"Beijing\"},{\"name\":\"Andy\",\"address\":\"ShangHai\"},{\"name\":\"Lisa\",\"address\":\"ShenZhen\"}],\"time\":\"2018-11-17\"}";
json_error_t error;
json_t *root;
json_t *location_arr;
json_t *location;
char *name, *address;
char *time;
int idx;
root = json_loads((const char*)text, 0, &error);
if(json_is_object(root))
{
LOG1("root format correct\r\n");
time = (char *)json_string_value(json_object_get(root, "time"));
location_arr = json_object_get(root, "location");
if(json_is_array(location_arr))
{
for(idx = 0; idx < json_array_size(location_arr); idx++)
{
l
没有合适的资源?快使用搜索试试~ 我知道了~
开源一些单片机常用的小工具
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共22个文件
c:6个
md:5个
lib:3个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 5 1 下载量 172 浏览量
2024-05-10
14:10:18
上传
评论
收藏 1.17MB ZIP 举报
温馨提示
SoftTimer 适用于单片机的软定时器 点击查看详情使用文档 支持自定义时间节拍 支持任意时间的回调 支持任意时间下的任意次数回调(也支持无限次) 支持一个任意类型的带参回调 支持运行中动态的添加定时器以及删除定时器 支持运行中动态暂定与重启定时器 支持任意添加的定时器自动排序(时间顺序) 单片机实现JSON构建与JSON解析 点击查看详情使用文档 构建json(支持普通无嵌套,支持嵌套json对象,支持嵌套包含数组的json对象) 解析json(支持普通常见数据类型的解析,支持值是一个对象的解析,支持值是一个数组的解析) Queue 适用于单片机的消息队列(可用于中断发送消息到队列) 点击查看详情使用文档 ..............
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 22 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/06b9d7cab2b746b0aa625e39a87bbebf_u011355389.jpg!1)
阿尔法星球
- 粉丝: 204
- 资源: 237
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)