Skip to content

Instantly share code, notes, and snippets.

@emersonmx
Created August 5, 2015 16:36
Show Gist options
  • Save emersonmx/44c0d26f4617b125477d to your computer and use it in GitHub Desktop.
Save emersonmx/44c0d26f4617b125477d to your computer and use it in GitHub Desktop.
Leitor de JSON usando JSONCPP :D
#include <iostream>
#include <fstream>
#include "json/json.h"
using namespace std;
void print_version(const Json::Value& root) {
cout << root["version"].asString() << endl;
}
void print_texture(const Json::Value& root) {
cout << root["texture"].asString() << endl;
}
void print_region_name(const Json::Value& region) {
cout << region["name"].asString() << endl;
}
void print_region(const Json::Value& region) {
const Json::Value values = region["region"];
cout << '[' << values[0] << ", " << values[1] << ", " << values[2] << ", " << values[3] << ']' << endl;
}
void print_regions(const Json::Value& root) {
const Json::Value regions = root.get("regions", Json::Value());
for (int i = 0; i < regions.size(); ++i) {
print_region_name(regions[i]);
print_region(regions[i]);
}
}
int main() {
Json::Value root;
ifstream atlas("test.json", ifstream::binary);
atlas >> root;
print_version(root);
print_texture(root);
print_regions(root);
return 0;
}
{
"version": "0.1",
"texture": "test.png",
"regions": [
{
"name": "tr1",
"region": [0, 0, 8, 8]
},
{
"name": "tr2",
"region": [8, 0, 8, 8]
},
{
"name": "tr3",
"region": [0, 8, 8, 8]
},
{
"name": "tr4",
"region": [8, 8, 8, 8]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment