-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.c
95 lines (86 loc) · 2.43 KB
/
json_test.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <string.h>
#include "json.h"
#define BUFSIZE 512
// path format is obj.key or array[i]
const char *json_get(json_object *obj, const char *path)
{
char key[BUFSIZE];
const char *end = path;
while (1) {
int len = 0;
while (*end != '.' && *end != '[' && *end != '\0') {
key[len++] = *end++;
}
key[len] = '\0';
if (len > 0) {
obj = json_object_object_get(obj, key);
}
if (obj == NULL) {
return NULL;
}
if (*end == '\0') {
break;
}
if (*end == '.') {
++end;
}
if (*end == '[') {
int idx = 0;
++end;
while (*end != ']') {
idx = idx * 10 + *end - '0';
++end;
}
obj = json_object_array_get_idx(obj, idx);
printf("%d.\n", idx);
if (obj == NULL) {
return NULL;
}
if (*(end + 1) == '.') {
end += 2;
} else {
++end;
}
}
}
return json_object_get_string(obj);
}
int json_get_int(json_object *obj, const char *path)
{
return atoi(json_get(obj, path));
}
int main(int argc, char **argv)
{
json_object *status, *problemset, *problems, *result, *problem, *contestid, *index, *stat, *count, *statistics;
MC_SET_DEBUG(1);
problemset = json_object_from_file("problemset.problems");
status = json_object_object_get(problemset, "status");
printf("status = %s\n", json_object_get_string(status));
result = json_object_object_get(problemset, "result");
problems = json_object_object_get(result, "problems");
statistics = json_object_object_get(result, "problemStatistics");
int i = 0;
int len1 = json_object_array_length(problems);
printf("len1 = %d.\n", len1);
for (i = 0; i < len1; ++i) {
problem = json_object_array_get_idx(problems, i);
stat = json_object_array_get_idx(statistics, i);
index = json_object_object_get(problem, "index");
contestid = json_object_object_get(problem, "contestId");
count = json_object_object_get(stat, "solvedCount");
printf("problem_id = %3s", json_object_get_string(contestid));
printf("%s\t", json_object_get_string(index));
printf("%d\n", json_object_get_int(count));
}
printf("status = %s\n", json_get(problemset, "status"));
printf("result = %s\n", json_get(problemset, "result"));
printf("result.problems = %s\n", json_get(problemset, "result.problems"));
printf("result.problems[0] = %s\n", json_get(problemset, "result.problems[0]"));
printf("result.problems[0] = %s\n", json_get(problems, "[0]"));
json_object_put(problemset);
return 0;
}