开源代码
自己封装的基础函数
代码
-
int print_tab(int i)
-
{
-
for (; i>0; --i)
-
{
-
printf("\t");
-
}
-
-
return 0;
-
}
-
-
int json_tree_print(struct json_object *obj, const char *key, int level)
-
{
-
json_type type = json_object_get_type(obj);
-
int i = 0;
-
-
-
//for object
-
if (type == json_type_object)
-
{
-
if (key)
-
{
-
print_tab(level);
-
printf("%s:\n", key);
-
}
-
print_tab(level);
-
printf("{\n");
-
level++;
-
-
json_object_object_foreach(obj, key, val) {
-
struct json_object *tmp_obj = val;
-
json_tree_print(tmp_obj, key, level);
-
}
-
-
level--;
-
print_tab(level);
-
printf("}\n");
-
-
}
-
//for array
-
else if ((type == json_type_array))
-
{
-
if (key)
-
{
-
print_tab(level);
-
printf("%s:\n", key);
-
}
-
-
print_tab(level);
-
printf("[\n");
-
level++;
-
-
for(i=0; i < json_object_array_length(obj); i++) {
-
struct json_object *array_obj = json_object_array_get_idx(obj, i);
-
json_tree_print(array_obj, NULL, level);
-
}
-
-
level--;
-
print_tab(level);
-
printf("]\n");
-
-
}
-
else
-
{
-
print_tab(level);
-
printf("%s:%s\n", key, json_object_to_json_string(obj));
-
}
-
-
return 0;
-
-
-
}
示例
-
int main(int argc, char **argv)
-
{
-
struct json_object *root_obj;
-
-
//init
-
root_obj = json_tokener_parse(
-
"{" \
-
" \"switch\": { " \
-
" \"ip\":\"192.168.1.1\"," \
-
" \"mac\":\"00:11:22:33:44:55\" " \
-
" }, " \
-
" \"vlan\": { " \
-
" \"algo-table\":{" \
-
" \"1\":{" \
-
" \"src-ip\":1," \
-
" \"src-l2-port\":0," \
-
" \"src-l4-port\":1" \
-
" }" \
-
" }," \
-
" \"vlan-table\":{" \
-
" \"1\":{"
-
" \"vid\":123, " \
-
" \"port\":\"1-7\" " \
-
" }" \
-
" }" \
-
" }" \
-
"}" \
-
);
-
-
json_tree_print(root_obj, NULL, 0);
-
-
//release resource
-
json_object_put(root_obj);
-
-
return 0;
-
-
}
示例的输出
-
{
-
switch:
-
{
-
ip:"192.168.1.1"
-
mac:"00:11:22:33:44:55"
-
}
-
vlan:
-
{
-
algo-table:
-
{
-
1:
-
{
-
src-ip:1
-
src-l2-port:0
-
src-l4-port:1
-
}
-
}
-
vlan-table:
-
{
-
1:
-
{
-
vid:123
-
port:"1-7"
-
}
-
}
-
}
-
}
查找指定的json对象
-
typedef struct _key_obj{
-
char *key;
-
struct json_object *obj;
-
struct json_object *parent_obj;
-
-
}key_obj;
-
-
/*
-
function description:
-
if the value of input str is : "vlan.algo-table.1.src-ip", the output top_node_buf will be "vlan"
-
if the value of input str is : "algo-table.1.src-ip", the output top_node_buf will be "algo-table"
-
*/
-
int get_top_node(const char *str, char *top_node_buf, int buf_len)
-
{
-
const char *p_start = NULL;
-
const char *p_end = NULL;
-
-
if (!str || !top_node_buf)
-
{
-
return -1;//fail
-
}
-
-
p_start = str;
-
p_end = strchr(str, '.');
-
-
if (!p_end)
-
{
-
snprintf(top_node_buf, buf_len, "%s", str);
-
}
-
else
-
{
-
int i = 0;
-
int len = p_end - p_start;
-
-
if ((len) > (buf_len-1))
-
{
-
return -1;//no enough buffer
-
}
-
-
for (i=0; i<len; ++i)
-
{
-
top_node_buf[i] = p_start[i];
-
}
-
top_node_buf[i] = '\0';;
-
}
-
-
return 0;
-
}
-
-
-
/*
-
return key and json object, so we can change the value
-
-
only work for json_type_object, for json_type_array, the function will not work
-
*/
-
int json_get_obj(struct json_object *obj, const char *str, key_obj* output_obj)
-
{
-
json_type type = json_object_get_type(obj);
-
char top_node[128] = {0};
-
const char *p = NULL;
-
int match = 0;
-
int i = 0;
-
-
if (!obj || !str)
-
{
-
return -1;
-
}
-
-
if (get_top_node(str, top_node, sizeof(top_node)) != 0)
-
{
-
return -1;
-
}
-
-
p = strchr(str, '.');
-
//for object
-
if (type == json_type_object)
-
{
-
json_object_object_foreach(obj, key, val) {
-
struct json_object *tmp_obj = val;
-
if (!strcmp(top_node, key))
-
{
-
match = 1;
-
//leaf now,return node obj
-
if (!p)
-
{
-
output_obj->key = key;
-
output_obj->obj = tmp_obj;
-
output_obj->parent_obj = obj;
-
return 0;
-
}
-
else
-
{
-
return json_get_obj(tmp_obj, ++p, output_obj);
-
}
-
break;
-
}
-
}
-
-
if (!match)
-
{
-
return -1;
-
}
-
}
-
-
return 0;
-
-
}
通过文件内容来构造json对象
有现成的函数:
-
struct json_object* json_object_from_file(char *filename)
把json对象转化成字符串并保存到文件中
有现成的函数:
-
int json_object_to_file(char *filename, struct json_object *obj)
从json对象中读取指定路径的值
代码
-
const char * json_get_value(struct json_object *root_obj, const char *str)
-
{
-
key_obj node;
-
-
if (json_get_obj(root_obj, str, &node) != 0)
-
{
-
return NULL;
-
}
-
-
return (const char *)json_object_get_string(node.obj);
-
}
示例
-
int main(int argc, char **argv)
-
{
-
struct json_object *root_obj;
-
-
//init
-
root_obj = json_tokener_parse(
-
"{" \
-
" \"switch\": { " \
-
" \"ip\":\"192.168.1.1\"," \
-
" \"mac\":\"00:11:22:33:44:55\" " \
-
" }, " \
-
" \"vlan\": { " \
-
" \"algo-table\":{" \
-
" \"1\":{" \
-
" \"src-ip\":1," \
-
" \"src-l2-port\":0," \
-
" \"src-l4-port\":1" \
-
" }" \
-
" }," \
-
" \"vlan-table\":{" \
-
" \"1\":{"
-
" \"vid\":123, " \
-
" \"port\":\"1-7\" " \
-
" }" \
-
" }" \
-
" }" \
-
"}" \
-
);
-
-
printf("get value:[%s]\n", json_get_value(root_obj, "vlan.vlan-table.1.port"));
-
-
//release resource
-
json_object_put(root_obj);
-
-
return 0;
-
-
}
输出:
在json树中修改指定路径的值
代码:
-
int json_set_value(struct json_object *root_obj, const char *str, const char *val)
-
{
-
key_obj node;
-
char key[64] = {0};
-
struct json_object *obj = NULL;
-
-
if (json_get_obj(root_obj, str, &node) != 0)
-
{
-
return -1;
-
}
-
-
obj = json_object_new_string(val);
-
snprintf(key, sizeof(key), "%s", node.key);
-
json_object_object_add(node.parent_obj, key, obj);
-
-
return 0;
-
}
示例:
-
int main(int argc, char **argv)
-
{
-
struct json_object *root_obj;
-
-
//init
-
root_obj = json_tokener_parse(
-
"{" \
-
" \"switch\": { " \
-
" \"ip\":\"192.168.1.1\"," \
-
" \"mac\":\"00:11:22:33:44:55\" " \
-
" }, " \
-
" \"vlan\": { " \
-
" \"algo-table\":{" \
-
" \"1\":{" \
-
" \"src-ip\":1," \
-
" \"src-l2-port\":0," \
-
" \"src-l4-port\":1" \
-
" }" \
-
" }," \
-
" \"vlan-table\":{" \
-
" \"1\":{"
-
" \"vid\":123, " \
-
" \"port\":\"1-7\" " \
-
" }" \
-
" }" \
-
" }" \
-
"}" \
-
);
-
-
printf("get value:[%s]\n", json_get_value(root_obj, "vlan.vlan-table.1.port"));
-
json_set_value(root_obj,"vlan.vlan-table.1.port", "8,9" );
-
printf("get value:[%s]\n", json_get_value(root_obj, "vlan.vlan-table.1.port"));
-
-
//release resource
-
json_object_put(root_obj);
-
-
return 0;
-
-
}
输出:
-
get value:[1-7]
-
get value:[8,9]
往json树的指定路径中添加新的对象
-
int json_add_json_obj(struct json_object *root_obj, const char *path,
-
const char *key, struct json_object *new_obj)
-
{
-
key_obj node;
-
-
if (json_get_obj(root_obj, path, &node) != 0)
-
{
-
return -1;
-
}
-
-
json_object_object_add(node.obj, key, new_obj);
-
-
return 0;
-
}
示例:
-
int main(int argc, char **argv)
-
{
-
struct json_object *root_obj, *new_obj;
-
-
-
int i;
-
int ret = -1;
-
-
//init
-
root_obj = json_tokener_parse(
-
"{" \
-
" \"switch\": { " \
-
" \"ip\":\"192.168.1.1\"," \
-
" \"mac\":\"00:11:22:33:44:55\" " \
-
" }, " \
-
" \"vlan\": { " \
-
" \"algo-table\":{" \
-
" \"1\":{" \
-
" \"src-ip\":1," \
-
" \"src-l2-port\":0," \
-
" \"src-l4-port\":1" \
-
" }" \
-
" }," \
-
" \"vlan-table\":{" \
-
" \"1\":{"
-
" \"vid\":123, " \
-
" \"port\":\"1-7\" " \
-
" }" \
-
" }" \
-
" }" \
-
"}" \
-
);
-
-
new_obj = json_tokener_parse(
-
"{" \
-
" \"vid\":234," \
-
" \"port\":\"8,9\"" \
-
"}" \
-
);
-
-
printf("-----------------before adding-----------------------------------\n");
-
json_tree_print(root_obj, NULL, 0);
-
-
json_add_json_obj(root_obj,"vlan.vlan-table","2",new_obj);
-
-
printf("-----------------after adding-----------------------------------\n");
-
json_tree_print(root_obj, NULL, 0);
-
-
//release resource
-
json_object_put(root_obj);
-
-
return 0;
-
-
}
输出:
-
-----------------before adding-----------------------------------
-
{
-
switch:
-
{
-
ip:"192.168.1.1"
-
mac:"00:11:22:33:44:55"
-
}
-
vlan:
-
{
-
algo-table:
-
{
-
1:
-
{
-
src-ip:1
-
src-l2-port:0
-
src-l4-port:1
-
}
-
}
-
vlan-table:
-
{
-
1:
-
{
-
vid:123
-
port:"1-7"
-
}
-
}
-
}
-
}
-
-----------------after adding-----------------------------------
-
{
-
switch:
-
{
-
ip:"192.168.1.1"
-
mac:"00:11:22:33:44:55"
-
}
-
vlan:
-
{
-
algo-table:
-
{
-
1:
-
{
-
src-ip:1
-
src-l2-port:0
-
src-l4-port:1
-
}
-
}
-
vlan-table:
-
{
-
1:
-
{
-
vid:123
-
port:"1-7"
-
}
-
2:
-
{
-
vid:234
-
port:"8,9"
-
}
-
}
-
}
-
}
在json树中删除指定路径的对象
代码:
-
int json_del_json_obj(struct json_object *root_obj, const char *path)
-
{
-
key_obj node;
-
-
if (json_get_obj(root_obj, path, &node) != 0)
-
{
-
return -1;
-
}
-
-
json_object_object_del(node.parent_obj, node.key);
-
-
return 0;
-
}
示例:
-
int main(int argc, char **argv)
-
{
-
struct json_object *root_obj, *new_obj;
-
-
-
int i;
-
int ret = -1;
-
-
//init
-
root_obj = json_tokener_parse(
-
"{" \
-
" \"switch\": { " \
-
" \"ip\":\"192.168.1.1\"," \
-
" \"mac\":\"00:11:22:33:44:55\" " \
-
" }, " \
-
" \"vlan\": { " \
-
" \"algo-table\":{" \
-
" \"1\":{" \
-
" \"src-ip\":1," \
-
" \"src-l2-port\":0," \
-
" \"src-l4-port\":1" \
-
" }" \
-
" }," \
-
" \"vlan-table\":{" \
-
" \"1\":{"
-
" \"vid\":123, " \
-
" \"port\":\"1-7\" " \
-
" }" \
-
" }" \
-
" }" \
-
"}" \
-
);
-
-
new_obj = json_tokener_parse(
-
"{" \
-
" \"vid\":234," \
-
" \"port\":\"8,9\"" \
-
"}" \
-
);
-
-
printf("-----------------before deleting-----------------------------------\n");
-
json_tree_print(root_obj, NULL, 0);
-
-
json_del_json_obj(root_obj,"vlan.vlan-table.1");
-
-
printf("-----------------after deleting-----------------------------------\n");
-
json_tree_print(root_obj, NULL, 0);
-
-
//release resource
-
json_object_put(root_obj);
-
-
return 0;
-
-
}
输出:
-
-----------------before deleting-----------------------------------
-
{
-
switch:
-
{
-
ip:"192.168.1.1"
-
mac:"00:11:22:33:44:55"
-
}
-
vlan:
-
{
-
algo-table:
-
{
-
1:
-
{
-
src-ip:1
-
src-l2-port:0
-
src-l4-port:1
-
}
-
}
-
vlan-table:
-
{
-
1:
-
{
-
vid:123
-
port:"1-7"
-
}
-
}
-
}
-
}
-
-----------------after deleting-----------------------------------
-
{
-
switch:
-
{
-
ip:"192.168.1.1"
-
mac:"00:11:22:33:44:55"
-
}
-
vlan:
-
{
-
algo-table:
-
{
-
1:
-
{
-
src-ip:1
-
src-l2-port:0
-
src-l4-port:1
-
}
-
}
-
vlan-table:
-
{
-
}
-
}
-
}
阅读(1119) | 评论(0) | 转发(0) |