Last active
June 25, 2021 13:35
-
-
Save komasaru/47f6df894e79f04f1a21 to your computer and use it in GitHub Desktop.
C++ source code to parse json datas by picojson.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* C++ source code to parse json datas by picojson. | |
*/ | |
#include <string> | |
#include <sstream> | |
#include <fstream> | |
#include <iostream> | |
#include "picojson.h" | |
using namespace std; | |
using namespace picojson; | |
int main() { | |
// Declaration | |
stringstream ss; | |
ifstream f; | |
unsigned int i; | |
// Read Json file | |
f.open("test.json", ios::binary); | |
if (!f.is_open()) return 1; | |
ss << f.rdbuf(); | |
f.close(); | |
// Parse Json data | |
value v; | |
ss >> v; | |
string err = get_last_error(); | |
if(!err.empty()) { | |
cerr << err << endl; | |
return -1; | |
} | |
object& o = v.get<object>()["Test1"].get<object>(); | |
cout << "TestInt: " << o["TestInt"].get<double>() << endl; | |
cout << "TestDbl: " << o["TestDbl"].get<double>() << endl; | |
cout << "TestStr: " << o["TestStr"].get<string>() << endl; | |
cout << "TestBln: " << o["TestBln"].get<bool>() << endl; | |
array& aAry = o["TestAry"].get<array>(); | |
cout << "TestAry:" << endl; | |
for (i = 0; i < aAry.size(); i++) | |
cout << "\t" << aAry[i].get<string>() << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
38 : reference to 'array' is ambiguous as it exists both in std, and picojson.
Other than that it's good code, helped me to get picojson syntax quickly.