官方网址:https://wiki.gnome.org/Projects/JsonGlib
代码地址:https://gitlab.gnome.org/GNOME/json-glib/-/tree/master/
可以下载下来看一些函数的定义和数据结构。
C下json操作的库有很多。这里介绍下json-glib。
Json-glib:c下序列化的一个库。基于glib,可以解析并生成json文件。
下载:
包含头文件:#include <json-glib/json-glib.h>
编译时加:pkg-config --cflags --libs json-glib-1.0
其中主要提供三种基本类型:JsonNode、 JsonArray 、JsonObject
JsonObject 就是Json中的对象。包含一个或多个健值对。比如{“name”:”john”, “age”:13}
也可以{“name”:”john”, “age”:23, “frieds”:[{“name”:”lili”},{“name”:”mark”}], 像这样包含数组。
JsonArray为数组:可以直接包含基本类型如整形,字符串,也可像上面”friends”那样包含几个object
JsonNode: 可以包含认可基本类型,或者是数组,或者对象。一般用在生成Json数据流的时候需要指定一个根结点。此时可把它们装在jsonnode中。
/**
* json_object_new: (constructor)
*
* Creates a new object.
*
* Returns: (transfer full): the newly created object
*/
JsonObject *
json_object_new (void)
{
JsonObject *object;
object = g_slice_new0 (JsonObject);
object->age = 0;
object->ref_count = 1;
object->members = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free,
(GDestroyNotify) json_node_unref);
g_queue_init (&object->members_ordered);
return object;
}
json_object_set_int_member
json_object_set_string_member
json_object_set_array_member
json_object_new
json_object_new
//generate
//编译命令:gcc -o generate generate.c `pkg-config --cflags --libs json-glib-1.0`
#include <json-glib/json-glib.h>
int main(int argc, char* argv[])
{
g_type_init();
JsonObject *obj = json_object_new();
json_object_set_string_member(obj, "name", "john");
json_object_set_int_member(obj, "age", 18);
JsonArray *arr = json_array_new();
json_array_add_string_element(arr, "lily");
json_array_add_string_element(arr, "mark");
json_object_set_array_member(obj, "friends", arr);
//generate
JsonGenerator *g = json_generator_new();
JsonNode *root = json_node_new(JSON_NODE_OBJECT);
json_node_set_object(root, obj);
json_generator_set_root(g, root);
json_generator_to_file(g, "ex.json", NULL);
return 0;
}
//prase
//编译命令:gcc -o prase prase.c `pkg-config --cflags --libs json-glib-1.0`
#include <json-glib/json-glib.h>
int main(int argc, char* argv[])
{
JsonParser *parser = json_parser_new();
JsonNode *node = json_node_new(JSON_NODE_OBJECT);
json_parser_load_from_file(parser, "ex.json", NULL);
node = json_parser_get_root(parser);
JsonObject *obj2 = json_object_new();
obj2 = json_node_get_object(node);
const char *aaa = json_object_get_string_member(obj2, "name");
const int aa = json_object_get_int_member(obj2, "age");
printf("%s\n", aaa);
printf("%d\n", aa);
JsonArray *array = json_array_new();
array = json_object_get_array_member(obj2, "friends");
const char *bbb = json_array_get_string_element(array, 0);
const char *bb = json_array_get_string_element(array, 1);
printf("%s\n", bbb);
printf("%s\n", bb);
return 0;
}
另外一个例子
#include <stdlib.h>
#include <stdio.h>
#include <glib-object.h>
#include <json-glib/json-glib.h>
//编译命令:gcc -o test3 test3.c `pkg-config --cflags --libs json-glib-1.0`
int main(void)
{
JsonBuilder *builder = json_builder_new();
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "array");
json_builder_begin_array(builder);
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "name");
json_builder_add_string_value(builder, "charles");
json_builder_set_member_name(builder, "age");
json_builder_add_int_value(builder, 20);
json_builder_end_object(builder);
json_builder_begin_object(builder);
json_builder_set_member_name(builder, "name");
json_builder_add_string_value(builder, "May");
json_builder_set_member_name(builder, "age");
json_builder_add_int_value(builder, 21);
json_builder_end_object(builder);
json_builder_end_array(builder);
json_builder_end_object(builder);
JsonNode *node = json_builder_get_root(builder);
g_object_unref( builder);
JsonGenerator *generator = json_generator_new();
json_generator_set_root(generator, node);
gchar *data = json_generator_to_data(generator, NULL);
printf("%s\n", data);
json_node_free(node);
g_object_unref(generator);
free(data);
return 0;
}