-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparsejson.c
More file actions
68 lines (47 loc) · 1.24 KB
/
parsejson.c
File metadata and controls
68 lines (47 loc) · 1.24 KB
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
#include <lvmx.h>
#include <jsmn.h>
int JSON_STRING[] = "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
int getElem(int* json, jsmntok_t* t, int* key, int size) {
int i;
int jsonitr = 1;
int pos = -1;
for (i = 0; i < size; i++) {
json[t[jsonitr].end] = 0;
if (strcmp(key, &json[t[jsonitr].start])) {
debuglog("found!\n");
pos = jsonitr + 1;
//break;
}
jsonitr += t[jsonitr].size + 1;
}
return pos;
}
int getString(int* json, jsmntok_t* t, int id, int* dest) {
if (t[id].type != JSMN_STRING) {
debuglog("String expected\n");
return -1;
}
json[t[id].end] = 0;
strcpy(dest, &json[t[id].start]);
return 0;
}
int main() {
setDVInt(DEVICEROOT_SLOT, "OVERCLOCK", 30);
debuglog("hello\n");
debuglog(JSON_STRING);
debuglog("\n");
jsmn_parser p;
jsmntok_t t[32]; /* We expect no more than 128 tokens */
jsmn_init(&p);
int r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t));
int textbuff[32];
if (t[0].type != JSMN_OBJECT) {
debuglog("Object expected\n");
return -1;
}
int size = t[0].size;
int tokItr = getElem(JSON_STRING, t, "user", size);
getString(JSON_STRING, t, tokItr, textbuff);
debuglog(textbuff);
return -1;
}