Skip to content

Instantly share code, notes, and snippets.

@komasaru komasaru/PicoJson.cpp
Last active Aug 29, 2019

Embed
What would you like to do?
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

This comment has been minimized.

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
You can’t perform that action at this time.