1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <stdlib.h>
#include <stdio.h>
#include <glib-object.h>
#include <json-glib/json-glib.h>
struct Book {
gint64 Sets;
gint64 Tlower;
char *Cleaner;
char *Remark;
};
int main() {
GError *error;
if (argc < 2) {
printf("Usage: test <filename.json>\n");
return EXIT_FAILURE;
}
JsonParser *parser = json_parser_new();
json_parser_load_from_file(parser, "demo.json", &error);
if(error) {
printf("Unable to parse `%s': %s\n", argv[1], error->message);
g_error_free(error);
g_object_unref(parser);
return EXIT_FAILURE;
}
JsonNode *root = json_parser_get_root(parser);
JsonObject *stuff = json_node_get_object(root);
struct Book Book1 = {
.Sets = json_object_get_int_member(stuff, "Sets"),
.Tlower = json_object_get_int_member(stuff, "Tlower"),
.Cleaner = strdup(json_object_get_string_member(stuff, "Cleaner")),
.Remark = strdup(json_object_get_string_member(stuff, "Remark"))
};
printf(
"Sets = %ld, Tlower = %ld, Cleaner = '%s', Remark = '%s'\n",
Book1.Sets, Book1.Tlower, Book1.Cleaner, Book1.Remark
);
g_object_unref(parser);
json_object_unref(root);
return EXIT_SUCCESS;
}
| |