ubus数据解析多层嵌套BLOBMSG_TYPE_TABLE或BLOBMSG_TYPE_ARRAY

  前言,项目需求,收到JSON数据,转为blob数据,然后信息解析,用uci接口,写入uci 文件的过程。网上没有搜到

一般都是一层解析,一对一的,很简单。但是我遇到了多层解析,如下:

第一层为: mac、2.4G、5G

第二层为: enable、channel、power、max_client_num

{“

    “mac”: ””,

    “2.4G”: {

        “enable”: 0

        “channel”: 0,

        “power”: 0,

        “max_client_num”: 0

    },

    “5G”: {

         “enable”: 0

        “channel”: 0,

        “power”: 0,

       “max_client_num”: 0

    }

}

 难题来,如何定义policy,如何解析?  我直接上图

定义policy

第一层解析后放入tb

第二层解析后放入sub_tb

至于后面的数据处理,你们应该知道怎么做了

更新,解析数据如下:

{
    "enable":1,
    "timeout":2,
    "retry":3,
    "frequency":60,
    "detectSaasTimeout":15,
    "wifiList":[
        {
            "ssid":"ssid",
            "key":"key",
            "band":"5G"
        }
    ],
    "saasList":[
        {
            "destUrl":"192.168.11.215",
            "destPort":8888
        }
    ]
}

方式一:

enum {
    SAAS_TABLE,
    SAAS_TABLE_MAX
};

enum {
    WIFI_TABLE,
    WIFI_TABLE_MAX
};

enum {
    SAAS_URL,
    SAAS_PORT,
    SAAS_MAX
};

enum {
    WIFI_SSID,
    WIFI_KEY,
    WI
  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ubus数据具有多层嵌套BLOBMSG_TYPE_TABLEBLOBMSG_TYPE_ARRAY类型时,可以使用递归的方式进行解析。下面是一个示例代码,演示了如何解析多层嵌套ubus数据: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libubox/blobmsg_json.h> void parse_blobmsg_table(const struct blob_attr *attr); void parse_blobmsg_array(const struct blob_attr *attr) { int rem; struct blob_attr *tb[BLOBMSG_TYPE_MAX + 1]; struct blob_attr *cur; blobmsg_for_each_attr(cur, attr, rem) { if (blobmsg_type(cur) == BLOBMSG_TYPE_TABLE) { parse_blobmsg_table(cur); } else if (blobmsg_type(cur) == BLOBMSG_TYPE_ARRAY) { parse_blobmsg_array(cur); } else { fprintf(stderr, "Unexpected blobmsg type\n"); } } } void parse_blobmsg_table(const struct blob_attr *attr) { int rem; struct blob_attr *tb[BLOBMSG_TYPE_MAX + 1]; struct blob_attr *cur; blobmsg_parse(tb, BLOBMSG_TYPE_MAX, blobmsg_data(attr), blobmsg_data_len(attr)); if (!tb[0]) { fprintf(stderr, "Failed to parse blobmsg table\n"); return; } blobmsg_for_each_attr(cur, tb[0], rem) { if (blobmsg_type(cur) == BLOBMSG_TYPE_TABLE) { parse_blobmsg_table(cur); } else if (blobmsg_type(cur) == BLOBMSG_TYPE_ARRAY) { parse_blobmsg_array(cur); } else { // Handle individual values // You can access the value using blobmsg_get_type() and blobmsg_get_* functions const char *key = blobmsg_name(cur); int val = blobmsg_get_u32(cur); printf("Key: %s, Value: %d\n", key, val); } } } int main() { // Example ubus data in JSON format const char *json_data = "{\"table\":{\"nested_table\":{\"array\":[1,2,3]},\"value\":123}}"; struct blob_attr *attr; struct blob_buf buf; blob_buf_init(&buf, 0); if (blobmsg_add_json_from_string(&buf, json_data) != 0) { fprintf(stderr, "Failed to parse JSON data\n"); return 1; } attr = buf.head; if (!attr) { fprintf(stderr, "Empty blobmsg data\n"); return 1; } parse_blobmsg_table(attr); return 0; } ``` 在这个示例代码中,我们使用了libubox库中的函数来解析ubus数据。首先,我们将JSON格式的ubus数据转换为blobmsg格式,然后使用parse_blobmsg_table函数进行解析。在解析过程中,如果遇到嵌套BLOBMSG_TYPE_TABLE类型或BLOBMSG_TYPE_ARRAY类型,就会递归调用相应的解析函数。 在parse_blobmsg_table函数中,我们使用了blobmsg_type和blobmsg_for_each_attr函数来遍历解析后的数据。对于嵌套BLOBMSG_TYPE_TABLEBLOBMSG_TYPE_ARRAY类型,我们会分别调用parse_blobmsg_table和parse_blobmsg_array函数进行解析。对于其他类型,我们可以使用blobmsg_get_*函数来获取具体的值。 请注意,这只是一个简单的示例代码,实际的解析过程可能更加复杂,需要根据具体的ubus数据结构和需求进行适当的调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值