Created
December 2, 2013 11:51
-
-
Save asus4/7748391 to your computer and use it in GitHub Desktop.
picojsonを使ってoF環境設定ファイルを読み書き ref: http://qiita.com/asus4/items/641fc63348c0dfa5c797
This file contains hidden or 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
{ | |
"LV":3, | |
"HP":1, | |
"MP":1, | |
"sleepy":false, | |
"hungry":true, | |
"status":"カレー食べたい" | |
} |
This file contains hidden or 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
//testapp.cpp | |
#include "ofUtils.h" | |
#include <fstream> | |
#include "picojson.h" | |
void setup() { | |
// load from bin/data/config.json | |
std::string path = ofToDataPath("config.json"); | |
std::ifstream file; | |
file.open(path.c_str()); | |
if(file.fail()) { | |
std::cout << "Failed to open file : " << path << std::endl; | |
return; | |
} | |
// to std::string | |
std::istreambuf_iterator<char> first(file); | |
std::istreambuf_iterator<char> last; | |
std::string json_str(first, last); | |
// parse json | |
picojson::value json; | |
std::string err; | |
picojson::parse(json, json_str.begin(), json_str.end(), &err); | |
picojson::object &o = json.get<picojson::object>(); | |
int lv = (int) o["LV"].get<double>(); | |
bool hungry = o["hungry"].get<bool>(); | |
std::string status = o["status"].get<std::string>(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment