Mingz技术博客

...

导航

jsoncpp操作 json

Cpp代码  收藏代码
  1. #include <iostream>  
  2.   
  3. //#include "json/json.h"  
  4.   
  5. #include "json_reader.cpp"  
  6. #include "json_value.cpp"  
  7. #include "json_writer.cpp"  
  8.   
  9. using namespace std;  
  10. using namespace Json;  
  11.   
  12. int main(int argc, char *argv[])  
  13. {  
  14.       
  15.     // Configuration options  
  16.     char *config_doc=" { \"encoding\" :\"UTF-8\",\"plug-ins\" : [\"python\",\"c++\",\"ruby\"],\"indent\" : { \"length\" : 3, \"use_space\": true }}";  
  17.   
  18.     Json::Value root;   // will contains the root value after parsing.  
  19.     Json::Reader reader;  
  20.     bool parsingSuccessful = reader.parse( config_doc, root );  
  21.     if ( !parsingSuccessful )  
  22.     {  
  23.         // report to the user the failure and their locations in the document.  
  24.         std::cout  << "Failed to parse configuration\n"  
  25.                    << reader.getFormattedErrorMessages();  
  26.         return 1;  
  27.     }  
  28.   
  29.     reader.parse(config_doc,root,false);     //解析出json放到json中  
  30.       
  31.     std::string encoding = root.get("encoding""GBK" ).asString();  
  32.     const Json::Value plugins = root["plug-ins"];  
  33.       
  34.     for(int index = 0; index < plugins.size(); ++index ){  
  35.             cout<<plugins[index];  
  36.     }  
  37.       
  38.     cout<< plugins;  
  39.     cout<<encoding<<endl;  
  40.       
  41.     root["encoding"] = "GB2312";  
  42.     root["indent"]["length"] =  5;  
  43.     root["indent"]["use_space"] = false;  
  44.                    
  45.     Json::StyledWriter writer;  
  46.       
  47.     std::string outputConfig = writer.write( root );   
  48.       
  49.     cout<<outputConfig;  
  50.                                       
  51.     system("PAUSE");  
  52.     return 0;  
  53. }  
 

posted on 2013-07-31 16:51  Mingz2013  阅读(365)  评论(0编辑  收藏  举报