著者:後藤 大地
C言語のJSON用ライブラリ「Jansson」の使い方を紹介する。Janssonライブラリは、使い方がかなり直感的で扱いやすい。C言語でJSONデータを扱いたいのなら最初に使っておきたいものだ。
記事本文掲載のシェルスクリプトマガジンvol.55は以下のリンク先でご購入できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{ "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": 100 }, "Animated" : false, "IDs": [116, 943, 234, 38793] } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[ { "precision": "zip", "Latitude": 37.7668, "Longitude": -122.3959, "Address": "", "City": "SAN FRANCISCO", "State": "CA", "Zip": "94107", "Country": "US" }, { "precision": "zip", "Latitude": 37.371991, "Longitude": -122.026020, "Address": "", "City": "SUNNYVALE", "State": "CA", "Zip": "94085", "Country": "US" } ] |
1 |
{"Image":{"Width":800,"Height":600,"Title":"View from 15th Floor","Thumbnail":{"Url":"http://www.example.com/image/481989943","Height":125,"Width":100},"Animated":false,"IDs":[116,943,234,38793]}} |
1 |
[{"precision":"zip","Latitude":37.7668,"Longitude":-122.3959,"Address":"","City":"SAN FRANCISCO","State":"CA","Zip":"94107","Country":"US"},{"precision":"zip","Latitude":37.371991,"Longitude":-122.026020,"Address":"","City":"SUNNYVALE","State":"CA","Zip":"94085","Country":"US"}] |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
#include <stdio.h> #include <stdlib.h> #include <jansson.h> void print_json_aux(json_t *element, int indent); const char *json_plural(int count) { return count == 1 ? "" : "s"; } void print_json_indent(int indent) { int i; for (i = 0; i < indent; i++) { putchar(' '); } } void print_json_object(json_t *element, int indent) { size_t size; const char *key; json_t *value; print_json_indent(indent); size = json_object_size(element); printf("JSON Object of %ld pair%s:\n", size, json_plural(size)); json_object_foreach(element, key, value) { print_json_indent(indent + 2); printf("JSON Key: \"%s\"\n", key); print_json_aux(value, indent + 2); } } void print_json_array(json_t *element, int indent) { size_t i; size_t size = json_array_size(element); print_json_indent(indent); printf("JSON Array of %ld element%s:\n", size, json_plural(size)); for (i = 0; i < size; i++) { print_json_aux(json_array_get(element, i), indent + 2); } } void print_json_string(json_t *element, int indent) { print_json_indent(indent); printf("JSON String: \"%s\"\n", json_string_value(element)); } void print_json_integer(json_t *element, int indent) { print_json_indent(indent); printf("JSON Integer: \"%" JSON_INTEGER_FORMAT "\"\n", json_integer_value(element)); } void print_json_real(json_t *element, int indent) { print_json_indent(indent); printf("JSON Real: %f\n", json_real_value(element)); } void print_json_true(json_t *element, int indent) { (void)element; print_json_indent(indent); printf("JSON True\n"); } void print_json_false(json_t *element, int indent) { (void)element; print_json_indent(indent); printf("JSON False\n"); } void print_json_null(json_t *element, int indent) { (void)element; print_json_indent(indent); printf("JSON Null\n"); } void print_json_aux(json_t *element, int indent) { switch (json_typeof(element)) { case JSON_OBJECT: print_json_object(element, indent); break; case JSON_ARRAY: print_json_array(element, indent); break; case JSON_STRING: print_json_string(element, indent); break; case JSON_INTEGER: print_json_integer(element, indent); break; case JSON_REAL: print_json_real(element, indent); break; case JSON_TRUE: print_json_true(element, indent); break; case JSON_FALSE: print_json_false(element, indent); break; case JSON_NULL: print_json_null(element, indent); break; default: fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element)); } } void print_json(json_t *root) { print_json_aux(root, 0); } json_t *load_json(const char *text) { json_t *root; json_error_t error; root = json_loads(text, 0, &error); if (root) { return root; } else { fprintf(stderr, "json error on line %d: %s\n", error.line, error.text); return (json_t *)0; } } char *read_line(char *line, int max_chars) { printf("Type some JSON > "); fflush(stdout); return fgets(line, max_chars, stdin); } int main(int argc, char *argv[]) { char line[4096]; if (argc != 1) { fprintf(stderr, "Usage: %s\n", argv[0]); exit(-1); } while (read_line(line, 4096) != (char *)NULL) { json_t *root = load_json(line); if (root) { print_json(root); json_decref(root); } } return 0; } |
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 |
#include <stdio.h> #include <stdlib.h> #include <jansson.h> int main(int argc, char *argv[]) { // JSONデータをパース json_error_t error; json_t *root = json_loads(buf, 0, &error); // JSON要素の種類判定 switch (json_typeof(root)) { case JSON_OBJECT: break; case JSON_ARRAY: break; case JSON_STRING: break; case JSON_INTEGER: break; case JSON_REAL: break; case JSON_TRUE: break; case JSON_FALSE: break; case JSON_NULL: break; default: } // JSONデータを印刷 printf("%s\n", json_string_value(element)); printf("%f\n", json_real_value(element)); printf("%lld\n", json_integer_value(element)); // 配列処理 int index; json_t *value; json_array_foreach(element, index, value) { printf("%d\n", index); ...value... } // オブジェクト処理 const char *key; json_t *value; json_object_foreach(element, key, value) { printf("%s\n", key); ...value... } // メモリー解放 json_decref(root); return 0; } |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <jansson.h> void print(json_t *element, int indent) { const char *key; json_t *value; int index; #define INDENT \ for (int i = 0; i < indent; i++) \ putchar(' '); switch (json_typeof(element)) { case JSON_OBJECT: putchar('\n'); json_object_foreach(element, key, value) { INDENT printf("%s: ", key); print(value, 1+indent); } break; case JSON_ARRAY: putchar('\n'); json_array_foreach(element, index, value) { INDENT printf("%d: ", index); print(value, 1+indent); } break; case JSON_STRING: printf("%s\n", json_string_value(element)); break; case JSON_INTEGER: printf("%lld\n", json_integer_value(element)); break; case JSON_REAL: printf("%f\n", json_real_value(element)); break; case JSON_TRUE: printf("true\n"); break; case JSON_FALSE: printf("false\n"); break; case JSON_NULL: printf("null\n"); break; } } int main(int argc, char *argv[]) { char buf[1024 * 1024]; int fd = open(argv[1], O_RDONLY); read(fd, buf, 1024 * 1024 - 1); // JSONデータをパース json_error_t error; json_t *root = json_loads(buf, 0, &error); // JSONデータをトラバースしながら印刷 print(root, 0); // JSONメモリー解放 json_decref(root); close(fd); return 0; } |