From 27cdc9cc0b943a4a32c6b137fe1fe22cea76d527 Mon Sep 17 00:00:00 2001 From: nezbednik Date: Fri, 21 Jul 2023 15:39:29 +0200 Subject: [PATCH] JSON parsing example --- source/main.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/source/main.c b/source/main.c index a30fc18..ac52100 100644 --- a/source/main.c +++ b/source/main.c @@ -92,15 +92,24 @@ int main(int argc, char **argv) { if (res.body[i] == '\n') { if (lastWasNewline) { - printf("Headers end at: %i\n", i); - int rBL = resBodyLength - i; char* body = malloc(rBL); for (int j = 0; j < rBL; j++) { body[j] = res.body[j + i + 1]; } - printf("\"%s\"\n", body); + json_error_t error; + json_t *root = json_loads(body, 0, &error); + if (!root || !json_is_array(root)) { + printf("failed decoding\n"); + } + else { + printf("%s\n", body); + for (int j = 0; j < json_array_size(root); j++) { + json_t *data = json_array_get(root, j); + printf("%s\n", json_string_value(data)); + } + } break; }