Skip to content

Instantly share code, notes, and snippets.

@orkun
Forked from komasaru/PicoJson.cpp
Created June 25, 2021 13:35
Show Gist options
  • Save orkun/589b9790387c34f56b851c367575bfa9 to your computer and use it in GitHub Desktop.
Save orkun/589b9790387c34f56b851c367575bfa9 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment