#include <json/writer.h> #include <json/json.h>
#ifdef _DEBUG #pragma comment(lib, "json_vc71_libmtd.lib") #else #pragma comment(lib, "json_vc71_libmt.lib") #endif
void pjson(Json::Value& v) { if ( true == v.isArray() ) { for (int i=0, max=v.size(); i<max; i++) { if ( true == v[i].isObject() ) pjson(v[i]); else printf(":%s \n", v[i].asString().c_str()); } return; } else if ( true == v.isObject() ) { Json::Value::Members members(v.getMemberNames());
for (Json::Value::Members::iterator it = members.begin(); it != members.end(); ++it) { printf("%s:", (*it).c_str()); pjson(v[(*it).c_str()]); } } else { printf(":%s \n", v.asString().c_str()); return; } }
int _tmain(int argc, _TCHAR* argv[]) { int nRet = 0; char* buffer = 0; long size = 0; FILE *file = fopen("setting.json", "rb");
if ( file == 0 ) { printf("Cannot open setting.json file \n"); return 0; }
fseek(file, 0, SEEK_END); size=ftell(file);
buffer = (char*)malloc(sizeof(char)*size+1); rewind( file ); if ( fread(buffer, sizeof(char), size, file) != size ) { printf("Failed to read json file \n"); } fclose (file);
Json::Reader reader; Json::Value root; bool ok = reader.parse(buffer, root);
free(buffer);
if ( reader.getFormattedErrorMessages().size() != 0 ) { printf("Read json error : %s \n", reader.getFormattedErrorMessages().c_str()); }
if ( reader.getStructuredErrors().size() != 0 ) { for ( int i=0; i<reader.getStructuredErrors().size(); i++) printf("Read json error : %s \n", reader.getStructuredErrors()[i].message.c_str()); }
unsigned short ui = root["address"]["aaa"].asUInt(); pjson(root);
return 0; } |