JSON parsing example

This commit is contained in:
nezbednik 2023-07-21 15:39:29 +02:00
parent 6f627a88aa
commit 27cdc9cc0b

View File

@ -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;
}