Keil环境下使用Jansson构建JSON——基于STM32F103

33 篇文章 3 订阅
14 篇文章 1 订阅


Jansson解析JSON示例: 使用Jansson库解析JSON

构建普通的JSON键值对

JSON字符串:

{
	"status": true,
	"age": 20,
	"score": 78.4,
	"name": "whik1194",
	"blog": "https://blog.csdn.net/whik1194",
	"wechat": "mcu149"
}

JSON构建函数:

#include "jansson_demo.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对象

JSON字符串:

{
	"success": "1",
	"result": {
		"timestamp": "1592640249",
		"datetime_1": "2020-06-20 16:04:09",
		"week_1": 6,
		"week_2": "Saturday"
	}
}

JSON构建函数:

#include "jansson_demo.h"

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对象

JSON字符串:

{
	"location": [{
		"name": "Faye",
		"address": "Beijing"
	}, {
		"name": "Andy",
		"address": "ShangHai"
	}, {
		"name": "Lisa",
		"address": "ShenZhen"
	}],
	"time": "2018-11-17"
}

JSON构建函数:

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;
}
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

whik1194

如果对你有帮助,欢迎打赏。谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值