json-glib的用法
#include <stdlib.h> #include <stdio.h> #include <glib-object.h> #include <json-glib/json-glib.h> 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; }
输出的结果 {"array":[{"name":"charles","age":20},{"name":"May","age":21}]}
———————————————— 版权声明:本文为CSDN博主「CaspianSea」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/CaspianSea/article/details/80636971
//generate
//另一个版本的
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 int main(int argc, char* argv[]) { g_type_init(); JsonParser *p = json_parser_new(); gboolean ret; ret = json_parser_load_from_file(parser, fill_name, NULL); if (!ret) {…} JsonNode *root = json_node_new(JSON_NODE_OBJECT); root = json_parser_get_root(parse); JsonObject *obj = json_object_new(); obj = json_node_get_object(root); // 以下可对obj进行get操作 … return 0; }
#include<json-glib/json-glib.h> #include<stdio.h> #include<string.h> int main() { //生成json 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");//{"name":"john","age":18,"friends":["lily","mark"]} json_array_add_string_element(arr, "mark"); json_object_set_array_member(obj, "friends", arr); 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); //解析!!!
/*{"name":"john","age":18,"friends":["lily","mark"]}*/
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; }