-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonParse.cpp
51 lines (46 loc) · 1.32 KB
/
JsonParse.cpp
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
#include "JsonParse.h"
using namespace std;
JSONObj::JSONObj() { }
JSONObj::JSONObj(std::string v) {
val = v;
}
string JSONObj::asString() {
if (val.find("\"") == -1)return "";
val = val.substr(val.find("\"") + 1);
val = val.substr(0, val.find("\""));
return val;
}
bool JSONObj::asBool() {
if (val.find("true") != -1)return true;
return false;
}
int JSONObj::asInteger() {
return atoi(trim(val).c_str());
}
JSON JsonParse(string json) {
JSON ret;
json = json.substr(json.find('{'));
json = json.substr(0, json.find('}')+1);
int commark, mark;
while ((commark = json.find(',')) != -1) {
string object = json.substr(0, commark);
mark = -1; while (object[++mark] != '"'&&mark < object.size());
string key = object.substr(mark + 1); key = key.substr(0, key.find('"'));
string val = trim(object.substr(object.find(':') + 1));
ret[key] = { val };
json = json.substr(commark + 1);
}
if ((commark = json.find('}')) != -1) {
string object = json.substr(0, commark);
mark = -1; while (object[++mark] != '"'&&mark < object.size());
string key = object.substr(mark + 1); key = key.substr(0, key.find('"'));
string val = trim(object.substr(object.find(':') + 1));
ret[key] = { val };
json = json.substr(commark + 1);
}
else return JSON();
return ret;
}
JSONObj &JSON::operator[](std::string key) {
return pool[key];
}