-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_parse.cc
51 lines (45 loc) · 1.3 KB
/
json_parse.cc
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
/*
* Copyright (c) 2017-2018 SYZ
*
* https://github.com/open-source-parsers/jsoncpp.git
* v1.8.4
*
* g++ json_parse.cc -std=c++11 -I$JSONCPP184_HOME/include
* -L$JSONCPP184_HOME/lib -ljsoncpp -o release/json_parse
*/
#include <json/json.h>
#include <fstream>
#include <iostream>
#include <memory>
int main(int argc, char* argv[]) {
int result = -1;
do {
std::ifstream f;
f.open("./test.json");
if (!f.is_open()) break;
f.seekg(0, std::ios::end);
int64_t flen = f.tellg();
f.seekg(0, std::ios::beg);
std::unique_ptr<char> buf(new char[flen]);
f.read(buf.get(), flen);
Json::CharReaderBuilder crb;
crb["collectComments"] = false;
std::unique_ptr<Json::CharReader> pcr(crb.newCharReader());
Json::Value v;
JSONCPP_STRING errs;
try {
if (!pcr->parse(buf.get(), buf.get() + flen, &v, &errs)) break;
std::cout << v["key1"].asString() << std::endl;
const Json::Value arrayObj = v["array"];
for (auto it : arrayObj) {
std::cout << it.asString() << std::endl;
}
std::cout << v["object"]["name"].asString() << std::endl;
} catch (const std::exception& e) {
std::cout << "Json::CharReader::parse(" << buf.get() << ") error"
<< std::endl;
}
result = 0;
} while (0);
return result;
}