Created
July 2, 2009 05:29
-
-
Save mattn/139281 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdlib.h> | |
#include <iostream> | |
#include "picojson.h" | |
using namespace std; | |
using namespace picojson; | |
int main(void) { | |
value v; | |
const char *text = "{\"val\":\"1\",\"arr\":[1,2,3],\"obj\":{\"foo\":\"bar\", \"bar\":\"baz\"}}"; | |
string err = parse(v, text, text + strlen(text)); | |
cout << "root is error? : " << (err.empty() ? "no" : "yes") << endl; | |
cout << "root is object?: " << (v.is<object>() ? "yes" : "no") << endl; | |
cout << "root is array? : " << (v.is<array>() ? "yes" : "no") << endl; | |
{ | |
// get /obj/foo | |
object o = v.get<object>()["obj"].get<object>(); | |
cout << "/obj[\"foo\"] is " << o["foo"].to_str() << endl; | |
// each object | |
// get /obj/* | |
object::iterator it; | |
for (it = o.begin(); it != o.end(); it++) { | |
cout << "/obj/" << it->first << " is " << it->second.to_str() << endl; | |
} | |
} | |
{ | |
// get /arr[2] | |
array a = v.get<object>()["arr"].get<array>(); | |
cout << "/arr[1] is " << a[1].get<double>() << endl; | |
// each array | |
// get /arr/* | |
array::iterator it; | |
int n = 0; | |
for (it = a.begin(); it != a.end(); it++, n++) { | |
cout << "/arr[" << n << "] is " << it->to_str() << endl; | |
} | |
} | |
return 0; | |
} | |
/* | |
root is error? : no | |
root is object?: yes | |
root is array? : no | |
/obj["foo"] is bar | |
/obj/bar is baz | |
/obj/foo is bar | |
/arr[1] is 2 | |
/arr[0] is 1.000000 | |
/arr[1] is 2.000000 | |
/arr[2] is 3.000000 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment