Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active June 25, 2021 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save komasaru/47f6df894e79f04f1a21 to your computer and use it in GitHub Desktop.
Save komasaru/47f6df894e79f04f1a21 to your computer and use it in GitHub Desktop.
C++ source code to parse json datas by picojson.
/*
* 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;
}
@RafalLauterbach
Copy link

RafalLauterbach commented Aug 29, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment