JSON parsing example
This commit is contained in:
parent
6f627a88aa
commit
27cdc9cc0b
@ -92,15 +92,24 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
if (res.body[i] == '\n') {
|
if (res.body[i] == '\n') {
|
||||||
if (lastWasNewline) {
|
if (lastWasNewline) {
|
||||||
printf("Headers end at: %i\n", i);
|
|
||||||
|
|
||||||
int rBL = resBodyLength - i;
|
int rBL = resBodyLength - i;
|
||||||
char* body = malloc(rBL);
|
char* body = malloc(rBL);
|
||||||
for (int j = 0; j < rBL; j++) {
|
for (int j = 0; j < rBL; j++) {
|
||||||
body[j] = res.body[j + i + 1];
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user